FB面经 数字字符字符串
2018-09-13 本文已影响0人
Anseis
比较高频面经
ab123
ab111
比较大小, 这题具体情况还是和面试官讨论
static int method(String s1, String s2) {
int i = 0;
int j = 0;
while (i < s1.length() && j < s2.length()) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(j);
if (Character.isDigit(c1) && Character.isDigit(c2)) {
int n1 = c1-'0';
int n2 = c2-'0';
i++;
j++;
while (i < s1.length() && Character.isDigit(s1.charAt(i))) {
n1 = n1*10 + s1.charAt(i) - '0';
i++;
}
while (j < s2.length() && Character.isDigit(s2.charAt(j))) {
n2 = n2*10 + s2.charAt(j) - '0';
j++;
}
if (n1 > n2) {
return 1;
} else if (n1 < n2) {
return -1;
}
} else if (Character.isDigit(c1) && !Character.isDigit(c2)) {
return -1;
} else if (!Character.isDigit(c1) && Character.isDigit(c2)) {
return 1;
} else {
if (c1 > c2) {
return 1;
} else if (c1 < c2) {
return -1;
} else {
i++;
j++;
}
}
}
if (i < s1.length()) {
return 1;
} else if (j < s2.length()) {
return -1;
}
return 0;
}