题解 | #参数解析#

参数解析

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

//这题真是考双指针。。。。
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String str = sc.nextLine();
            char[] chars = str.toCharArray();
            List<String> list = new ArrayList<>();
            int j = 0;
            for (int i = 0; i < chars.length - 1; i++) {
                char ch = chars[i];
                if (ch == ' ') {
                    if ("".equals(str.substring(j, i))) {
                        j = i + 1;
                        if (chars[j] == '"') {
                            i = j;
                            j = j + 1;
                            for (; j < chars.length; j++) {
                                if (chars[j] == '"') {
                                    list.add(str.substring(i + 1, j));
                                    i = j;
                                    j = i + 1;
                                    break;
                                }
                            }
                        }
                    } else {
                        list.add(str.substring(j, i));
                        j = i + 1;
                        if (chars[j] == '"') {
                            i = j;
                            j = j + 1;
                            for (; j < chars.length; j++) {
                                if (chars[j] == '"') {
                                    list.add(str.substring(i + 1, j));
                                    i = j;
                                    j = i + 1;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (j >= str.length()) {
                System.out.println(list.size());
                for (String s : list) {
                    System.out.println(s);
                }
            }else{
                list.add(str.substring(j));
                System.out.println(list.size());
                for (String s : list) {
                    System.out.println(s);
                }
            }
        }
    }
}
全部评论

相关推荐

10-04 17:25
门头沟学院 Java
snqing:Java已经饱和了,根本不缺人。随便一个2000工资的都200人起投递
点赞 评论 收藏
分享
3 1 评论
分享
牛客网
牛客企业服务