Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitac40cc8

Browse files
committed
fix(1047): 新增双指针java写法
1 parentf8047a8 commitac40cc8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎problems/1047.删除字符串中的所有相邻重复项.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,29 @@ class Solution {
171171
}
172172
```
173173

174+
拓展:双指针
175+
```java
176+
classSolution {
177+
publicStringremoveDuplicates(Strings) {
178+
char[] ch= s.toCharArray();
179+
int fast=0;
180+
int slow=0;
181+
while(fast< s.length()){
182+
// 直接用fast指针覆盖slow指针的值
183+
ch[slow]= ch[fast];
184+
// 遇到前后相同值的,就跳过,即slow指针后退一步,下次循环就可以直接被覆盖掉了
185+
if(slow>0&& ch[slow]== ch[slow-1]){
186+
slow--;
187+
}else{
188+
slow++;
189+
}
190+
fast++;
191+
}
192+
returnnewString(ch,0,slow);
193+
}
194+
}
195+
```
196+
174197
Python:
175198
```python3
176199
classSolution:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp