leetcode-26-remove duplicates from sorted array

[LeetCode #26 Remove Duplicates from Sorted Array]

太简单,强行用一下指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int (int* nums, int numsSize) {
if (!numsSize){
return 0;
}
int length = 1;
int temp;
int *iter = nums;
for(int i = 0;i < numsSize-1; i++){
if (nums[1] != iter[length-1]){
temp = iter[length];
iter[length] = iter[i+1];
iter[i+1] = temp;
length ++;
}
nums = nums + 1;
}
return length;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def (self, nums):
"""
:type nums: List[int]
:rtype: int
"""
l = len(nums)
if l == 0: return 0
length = 1
for i in range(l-1):
current = nums[length-1]
_next = nums[i+1]
if current != _next:
temp = nums[length]
nums[length] = nums[i+1]
nums[i+1] = temp
length += 1
return length