mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
fix(1047): 新增双指针java写法
This commit is contained in:
@ -171,6 +171,29 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
拓展:双指针
|
||||
```java
|
||||
class Solution {
|
||||
public String removeDuplicates(String s) {
|
||||
char[] ch = s.toCharArray();
|
||||
int fast = 0;
|
||||
int slow = 0;
|
||||
while(fast < s.length()){
|
||||
// 直接用fast指针覆盖slow指针的值
|
||||
ch[slow] = ch[fast];
|
||||
// 遇到前后相同值的,就跳过,即slow指针后退一步,下次循环就可以直接被覆盖掉了
|
||||
if(slow > 0 && ch[slow] == ch[slow - 1]){
|
||||
slow--;
|
||||
}else{
|
||||
slow++;
|
||||
}
|
||||
fast++;
|
||||
}
|
||||
return new String(ch,0,slow);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
```python3
|
||||
class Solution:
|
||||
|
Reference in New Issue
Block a user