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

Commit e8be805

Browse files
ybian19azl397985856
authored andcommitted
feat: 26.remove-duplicates-from-sorted-array add Python3 implemen... (azl397985856#71)
1 parent e7360ef commit e8be805

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

‎problems/26.remove-duplicates-from-sorted-array.md‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ for (int i = 0; i < len; i++) {
6767

6868
## 代码
6969

70+
* 语言支持:JS,Python
71+
72+
Javascript Code:
7073
```js
7174
/*
7275
* @lc app=leetcode id=26 lang=javascript
@@ -83,51 +86,51 @@ for (int i = 0; i < len; i++) {
8386
*
8487
* Given a sorted array nums, remove the duplicates in-place such that each
8588
* element appear only once and return the new length.
86-
*
89+
*
8790
* Do not allocate extra space for another array, you must do this by modifying
8891
* the input array in-place with O(1) extra memory.
89-
*
92+
*
9093
* Example 1:
91-
*
92-
*
94+
*
95+
*
9396
* Given nums = [1,1,2],
94-
*
97+
*
9598
* Your function should return length = 2, with the first two elements of nums
9699
* being 1 and 2 respectively.
97-
*
100+
*
98101
* It doesn't matter what you leave beyond the returned length.
99-
*
102+
*
100103
* Example 2:
101-
*
102-
*
104+
*
105+
*
103106
* Given nums = [0,0,1,1,1,2,2,3,3,4],
104-
*
107+
*
105108
* Your function should return length = 5, with the first five elements of nums
106109
* being modified to 0, 1, 2, 3, and 4 respectively.
107-
*
110+
*
108111
* It doesn't matter what values are set beyond the returned length.
109-
*
110-
*
112+
*
113+
*
111114
* Clarification:
112-
*
115+
*
113116
* Confused why the returned value is an integer but your answer is an array?
114-
*
117+
*
115118
* Note that the input array is passed in by reference, which means
116119
* modification to the input array will be known to the caller as well.
117-
*
120+
*
118121
* Internally you can think of this:
119-
*
120-
*
122+
*
123+
*
121124
* // nums is passed in by reference. (i.e., without making a copy)
122125
* int len = removeDuplicates(nums);
123-
*
126+
*
124127
* // any modification to nums in your function would be known by the caller.
125128
* // using the length returned by your function, it prints the first len
126129
* elements.
127130
* for (int i = 0; i < len; i++) {
128131
* print(nums[i]);
129132
* }
130-
*
133+
*
131134
*/
132135
/**
133136
* @param {number[]} nums
@@ -140,8 +143,23 @@ var removeDuplicates = function(nums) {
140143
if (nums[fastP] !== nums[slowP]) {
141144
slowP++;
142145
nums[slowP] = nums[fastP]
143-
}
146+
}
144147
}
145148
return slowP + 1;
146149
};
147150
```
151+
152+
Python Code:
153+
```python
154+
class Solution:
155+
def removeDuplicates(self, nums: List[int]) -> int:
156+
if nums:
157+
slow = 0
158+
for fast in range(1, len(nums)):
159+
if nums[fast] != nums[slow]:
160+
slow += 1
161+
nums[slow] = nums[fast]
162+
return slow + 1
163+
else:
164+
return 0
165+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /