0 0 投票数

LeetCode 844 比较含退格的字符串

### 完整代码

bool backspaceCompare(char * S, char * T){
int i = strlen(S) - 1;
int j = strlen(T) - 1;
int skipS = 0;
int skipT = 0;
while (i >= 0 || j >= 0) {
while (i >= 0 && S[i] == '#') {
while (i >= 0 && S[i] == '#') {
skipS++;
i--;
}
while (i >= 0 && skipS > 0) {
if (S[i] == '#') {
skipS++;
} else {
skipS--;
}
i--;
}
}
while (j >= 0 && T[j] == '#') {
while (j >= 0 && T[j] == '#') {
skipT++;
j--;
}
while (j >= 0 && skipT > 0) {
if (T[j] == '#') {
skipT++;
} else {
skipT--;
}
j--;
}
}
if (i >=0 && j >= 0) {
if (S[i] != T[j]) {
return false;
} else {
i--;
j--;
}
} else {
return i < 0 && j < 0;
}
}
return true;
}

class Solution {
public boolean backspaceCompare(String S, String T) {
int i = S.length() - 1, j = T.length() - 1;
int skipS = 0, skipT = 0;

while (i >= 0 || j >= 0) { // While there may be chars in build(S) or build (T)
while (i >= 0) { // Find position of next possible char in build(S)
if (S.charAt(i) == '#') {skipS++; i--;}
else if (skipS > 0) {skipS--; i--;}
else break;
}
while (j >= 0) { // Find position of next possible char in build(T)
if (T.charAt(j) == '#') {skipT++; j--;}
else if (skipT > 0) {skipT--; j--;}
else break;
}
// If two actual characters are different
if (i >= 0 && j >= 0 && S.charAt(i) != T.charAt(j))
return false;
// If expecting to compare char vs nothing
if ((i >= 0) != (j >= 0))
return false;
i--; j--;
}
return true;
}
}

0 0 投票数