char to number不能用Integer.valueOf()
应该用 char - '0'
21.75 %
class StringIterator {
char[] chars;
char curr;
int count;
int pointer;
public StringIterator(String compressedString) {
this.chars = compressedString.toCharArray();
count = 0;
pointer = 0;
}
public char next() {
char result;
if (hasNext()) {
count--;
result = curr;
} else {
result = ' ';
}
return result;
}
public boolean hasNext() {
if (count != 0) {
return true;
}
if (pointer == chars.length) {
return false;
}
curr = chars[pointer++];
while (pointer < chars.length && Character.isDigit(chars[pointer])) {
count = count * 10 + chars[pointer] - '0';
pointer++;
}
return count != 0;
}
}
No comments:
Post a Comment