用java改写直播中的C代码 部分 case 无法通过
原C 代码:
int a = i == x ? 0 : atoi(s1.substring(i, x - i).c_str()); 中 c_Str()函数的作用是什么?
错误的代码:
public int compareVersion(String s1, String s2) {
int i = 0;
int j = 0;
while (i < s1.length() || j < s2.length()) {
int x = i, y = j;
while (x < s1.length() && s1.charAt(x) != '.')
x++;
while (y < s2.length() && s2.charAt(y) != '.')
y++;
int a = i == x ? 0 : Integer.valueOf(s1.substring(i, x - i));
int b = j == y ? 0 : Integer.valueOf(s2.substring(j, y - j));
if (a > b)
return 1;
if (a < b)
return -1;
i = x + 1;
j = y + 1;
}
return 0;
}
编译器报了什么错误?
✘ testcase: ‘“1.0”\n”1.1”’
bug处:
int a = i == x ? 0 : Integer.valueOf(s1.substring(i, x - i));
Line 18: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
提问于2019-08-31 09:33