mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
添加0844.比较含退格的字符串 python go双指针版本
This commit is contained in:
@ -205,6 +205,42 @@ class Solution:
|
||||
return self.get_string(s) == self.get_string(t)
|
||||
pass
|
||||
```
|
||||
双指针
|
||||
```python
|
||||
class Solution:
|
||||
def backspaceCompare(self, s: str, t: str) -> bool:
|
||||
s_index, t_index = len(s) - 1, len(t) - 1
|
||||
s_backspace, t_backspace = 0, 0 # 记录s,t的#数量
|
||||
while s_index >= 0 or t_index >= 0: # 使用or,以防长度不一致
|
||||
while s_index >= 0: # 从后向前,消除s的#
|
||||
if s[s_index] == '#':
|
||||
s_index -= 1
|
||||
s_backspace += 1
|
||||
else:
|
||||
if s_backspace > 0:
|
||||
s_index -= 1
|
||||
s_backspace -= 1
|
||||
else:
|
||||
break
|
||||
while t_index >= 0: # 从后向前,消除t的#
|
||||
if t[t_index] == '#':
|
||||
t_index -= 1
|
||||
t_backspace += 1
|
||||
else:
|
||||
if t_backspace > 0:
|
||||
t_index -= 1
|
||||
t_backspace -= 1
|
||||
else:
|
||||
break
|
||||
if s_index >= 0 and t_index >= 0: # 后半部分#消除完了,接下来比较当前位的值
|
||||
if s[s_index] != t[t_index]:
|
||||
return False
|
||||
elif s_index >= 0 or t_index >= 0: # 一个字符串找到了待比较的字符,另一个没有,返回False
|
||||
return False
|
||||
s_index -= 1
|
||||
t_index -= 1
|
||||
return True
|
||||
```
|
||||
|
||||
### Go
|
||||
|
||||
@ -226,6 +262,51 @@ func backspaceCompare(s string, t string) bool {
|
||||
return getString(s) == getString(t)
|
||||
}
|
||||
|
||||
```
|
||||
双指针
|
||||
```go
|
||||
func backspaceCompare(s string, t string) bool {
|
||||
s_index, t_index := len(s) - 1, len(t) - 1
|
||||
s_backspace, t_backspace := 0, 0 // 记录s,t的#数量
|
||||
for s_index >= 0 || t_index >= 0 { // 使用or,以防长度不一致
|
||||
for s_index >= 0 { // 从后向前,消除s的#
|
||||
if s[s_index] == '#' {
|
||||
s_index--
|
||||
s_backspace++
|
||||
} else {
|
||||
if s_backspace > 0 {
|
||||
s_index--
|
||||
s_backspace--
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
for t_index >= 0 { // 从后向前,消除t的#
|
||||
if t[t_index] == '#' {
|
||||
t_index--
|
||||
t_backspace++
|
||||
} else {
|
||||
if t_backspace > 0 {
|
||||
t_index--
|
||||
t_backspace--
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if s_index >= 0 && t_index >= 0 { // 后半部分#消除完了,接下来比较当前位的值
|
||||
if s[s_index] != t[t_index] {
|
||||
return false
|
||||
}
|
||||
} else if s_index >= 0 || t_index >= 0 { // 一个字符串找到了待比较的字符,另一个没有,返回false
|
||||
return false
|
||||
}
|
||||
s_index--
|
||||
t_index--
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
||||
|
||||
### JavaScript
|
||||
|
Reference in New Issue
Block a user