Skip to content

Commit 3083529

Browse files
committed
solve problem Remove Duplicates From Sorted Array
1 parent 32036c8 commit 3083529

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ All solutions will be accepted!
121121
|405|[Convert A Number To Hexadecimal](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/description/)|[java/py/js](./algorithms/ConvertANumberToHexadecimal)|Easy|
122122
|796|[Rotate String](https://leetcode-cn.com/problems/rotate-string/description/)|[java/py/js](./algorithms/RotateString)|Easy|
123123
|844|[Backspace String Compare](https://leetcode-cn.com/problems/backspace-string-compare/description/)|[java/py/js](./algorithms/BackspaceStringCompare)|Easy|
124+
|26|[Remove Duplicates From Sorted Array](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/)|[java/py/js](./algorithms/RemoveDuplicatesFromSortedArray)|Easy|
124125

125126
# Database
126127
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Remove Duplicates From Sorted Array
2+
This problem is easy to solve by double pointers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
if (nums.length == 0) return 0;
4+
5+
int pre = 0;
6+
7+
for (int i = 0; i < nums.length; i++) {
8+
if (i != pre && nums[i] != nums[pre]) {
9+
nums[++pre] = nums[i];
10+
}
11+
}
12+
13+
return pre + 1;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var removeDuplicates = function(nums) {
6+
if (nums.length === 0) return 0
7+
8+
let pre = 0
9+
10+
for (let i = 0; i < nums.length; i++) {
11+
if (i !== pre && nums[i] !== nums[pre]) {
12+
nums[++pre] = nums[i]
13+
}
14+
}
15+
16+
return pre + 1
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def removeDuplicates(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
if len(nums) == 0:
8+
return 0
9+
10+
pre = 0
11+
for i in range(len(nums)):
12+
if i != pre and nums[i] != nums[pre]:
13+
pre += 1
14+
nums[pre] = nums[i]
15+
16+
return pre + 1
17+

0 commit comments

Comments
 (0)