题解 | #参数解析#

参数解析

https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677


1、遍历字符串
2、将字符放入栈中,遇到空格,弹出栈
3、遇到两次引号,第二次弹出引号间的内容
4、如果第一个字符是引号,单独处理
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String cli = in.nextLine();
        Stack<Character> sta = new Stack<>();

        List<String> result = new ArrayList<>();

        Boolean isput = true;
        for(int i = 0;i<cli.length();i++){
            // System.out.println("i="+i+". "+cli.charAt(i));
            if(i==0){
                while(cli.charAt(i)!=' '){
                    //进
                    sta.push(cli.charAt(i));
                    i++;
                }
                i--;
                //出
                StringBuffer sb0 = new StringBuffer();
                Character tmp0;
                
                while(sta.size()!=0){
                    tmp0 = sta.pop();
                    sb0.append(String.valueOf(tmp0));
                }
                result.add(sb0.reverse().toString());
            }
            switch(cli.charAt(i)){
                case ' ':
                    sta.push(cli.charAt(i));
                    i++;
                    if(cli.charAt(i)=='"'){
                        sta.push(cli.charAt(i));
                        i++;
                        while(cli.charAt(i)!='\"'){
                            //进
                            sta.push(cli.charAt(i));
                            i++;
                        }
                        //出
                        StringBuffer sb = new StringBuffer();
                        Character tmp;
                        while( i<cli.length()&&(tmp = sta.pop()) !='\"'){
                            if(String.valueOf(tmp)!="\""){
                                sb.append(String.valueOf(tmp));
                            }
                            
                        }
                        result.add(sb.reverse().toString());
                        continue;
                    }
                    while(i<cli.length()&&cli.charAt(i)!=' '){
                        //进
                        sta.push(cli.charAt(i));
                        i++;
                    }
                    i--;
                    //出
                    StringBuffer sb2 = new StringBuffer();
                    Character tmp2;
                    while( (tmp2 = sta.pop()) !=' '){
                        sb2.append(String.valueOf(tmp2));
                    }
                    result.add(sb2.reverse().toString());
                    continue;
            }
        }
        System.out.println(result.size());
        result.forEach(System.out::println);
        
        
    }
}


全部评论

相关推荐

11-08 13:58
门头沟学院 Java
程序员小白条:竟然是蓝桥杯人才doge,还要花钱申领的offer,这么好的公司哪里去找
点赞 评论 收藏
分享
三年之期已到我的offer快到碗里来:9硕都比不上9本
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务