-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlintcode_0100.py
32 lines (28 loc) · 1.4 KB
/
lintcode_0100.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 100 · Remove Duplicates from Sorted Array 删除排序数组中的重复数字
# https://www.lintcode.com/problem/100/
# Given a sorted array, 'remove' the duplicates in place such that each element appear only once and return the 'new' length.
# Do not allocate extra space for another array, you must do this in place with constant memory. We will determine correctness by the length k of the returned array, intercepting the first k elements of the array.
class Solution:
"""
@param: nums: An ineger array
@return: An integer
"""
def removeDuplicates(self, nums):
# write your code here
print(nums)
if(nums==[]):return 0
cInd=0
for i in range(1,len(nums)):
if(nums[cInd]!=nums[i]):
if(i-cInd!=1):
nums[cInd+1]=nums[i]
cInd += 1
del nums[cInd+1:]
return cInd + 1
def main():
S=Solution()
nums=[1,1,2];print('----before ----nums=',nums);rLen=S.removeDuplicates(nums);print('----after ----nums:',rLen,'; nums=',nums)
nums=[1,1,2,3,4,4,4,4,5,6];print('----before ----nums=',nums);rLen=S.removeDuplicates(nums);print('----after ----nums:',rLen,'; nums=',nums)
nums=[1,1,2,3,6,6,6,8,12,12];print('----before ----nums=',nums);rLen=S.removeDuplicates(nums);print('----after ----nums:',rLen,'; nums=',nums)
if __name__ == "__main__":
main()