Skip to content

Commit 28b6687

Browse files
committed
better solution of Remove Duplicates From Sorted Array
1 parent a53878f commit 28b6687

File tree

1 file changed

+20
-1
lines changed
  • algorithms/RemoveDuplicatesFromSortedArray

1 file changed

+20
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
11
# Remove Duplicates From Sorted Array
2-
This problem is easy to solve by double pointers
2+
This problem is easy to solve by double pointers, like below:
3+
```python
4+
class Solution(object):
5+
def removeDuplicates(self, nums):
6+
"""
7+
:type nums: List[int]
8+
:rtype: int
9+
"""
10+
if len(nums) == 0:
11+
return 0
12+
13+
pre = 0
14+
for i in range(len(nums)):
15+
if nums[i] != nums[pre]:
16+
pre += 1
17+
nums[pre] = nums[i]
18+
19+
return pre + 1
20+
21+
```

0 commit comments

Comments
 (0)