华为9.11笔试第二题AC代码
import java.util.Scanner;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String originS = sc.nextLine();
String[] splitS = originS.split(" ");
LinkedList<String> list = new LinkedList<String>();
for (int i = 0; i < splitS.length; i++) {
String[] tmp = process2(splitS[i]);
for (int j = 0; j < tmp.length; j++) {
if (tmp[j] != "")
list.addFirst(tmp[j]);
}
}
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
}
public static String[] process2(String s) {
String newS = "";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-')
newS = newS + c;
else
newS = newS + '-';
}
String[] res = newS.split("-{2,}");
String[] newRes = new String[res.length];
for (int i = 0; i < res.length; i++)
newRes[i] = process3(res[i]);
return newRes;
}
public static String process3(String s) {
String res = "";
boolean inner = false;
int i = 0;
while (i < s.length() && s.charAt(i) == '-')
i++;
int j = s.length()-1;
while (j >= 0 && s.charAt(j) == '-')
j--;
for (int k = i; k <=j; k++)
res = res + s.charAt(k);
return res;
}
} #华为##笔试题目#
查看30道真题和解析