题解 | #参数解析#
参数解析
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);
}
}
}
}
}