力扣-844题(Java)
发布日期:2021-05-10 02:26:59 浏览次数:19 分类:精选文章

本文共 1106 字,大约阅读时间需要 3 分钟。

class Solution {

public boolean backspaceCompare(String s, String t) {
int i = 0, j = 0, len1 = s.length(), len2 = t.length(),
pos1 = 0, pos2 = 0;
Character[] array1 = new Character[len1];
Character[] array2 = new Character[len2];

while (i < len1) {  
if (s.charAt(i) == '#') {
if (pos1 > 0) {
pos1--;
} else {
array1[pos1++] = s.charAt(i);
}
} else {
array1[pos1++] = s.charAt(i);
}
i++;
}
while (j < len2) {
if (t.charAt(j) == '#') {
if (pos2 > 0) {
pos2--;
} else {
array2[pos2++] = t.charAt(j);
}
} else {
array2[pos2++] = t.charAt(j);
}
j++;
}
if (pos1 != pos2) {
return false;
}
i = 0;
while (i < len2) {
if (i >= array1.length || j >= array2.length) {
return false;
}
if (array1[pos1++] != array2[pos2++]) {
return false;
}
}
return true;
}

}

上一篇:力扣-167题(Java)-双指针
下一篇:力扣-283题(Java)-双指针

发表评论

最新留言

很好
[***.229.124.182]2025年04月28日 14时19分50秒