mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge branch 'youngyangyang04:master' into remote
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
|
||||
|
@ -209,6 +209,61 @@ var reverseLeftWords = function(s, n) {
|
||||
};
|
||||
```
|
||||
|
||||
版本二(在原字符串上操作):
|
||||
|
||||
```js
|
||||
/**
|
||||
* @param {string} s
|
||||
* @param {number} n
|
||||
* @return {string}
|
||||
*/
|
||||
var reverseLeftWords = function (s, n) {
|
||||
/** Utils */
|
||||
function reverseWords(strArr, start, end) {
|
||||
let temp;
|
||||
while (start < end) {
|
||||
temp = strArr[start];
|
||||
strArr[start] = strArr[end];
|
||||
strArr[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
/** Main code */
|
||||
let strArr = s.split('');
|
||||
let length = strArr.length;
|
||||
reverseWords(strArr, 0, length - 1);
|
||||
reverseWords(strArr, 0, length - n - 1);
|
||||
reverseWords(strArr, length - n, length - 1);
|
||||
return strArr.join('');
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function reverseLeftWords(s: string, n: number): string {
|
||||
/** Utils */
|
||||
function reverseWords(strArr: string[], start: number, end: number): void {
|
||||
let temp: string;
|
||||
while (start < end) {
|
||||
temp = strArr[start];
|
||||
strArr[start] = strArr[end];
|
||||
strArr[end] = temp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
/** Main code */
|
||||
let strArr: string[] = s.split('');
|
||||
let length: number = strArr.length;
|
||||
reverseWords(strArr, 0, length - 1);
|
||||
reverseWords(strArr, 0, length - n - 1);
|
||||
reverseWords(strArr, length - n, length - 1);
|
||||
return strArr.join('');
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
|
Reference in New Issue
Block a user