Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once.(巨大前提是数据广义单增) The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums. Return k.
1 2 3 4 5 6 7 8
classSolution(object): defremoveDuplicates(self, nums): dummy=1 for i inrange(1,len(nums)): if nums[i]!=nums[i-1]: nums[dummy]=nums[i] dummy+=1 return dummy
(27)Remove Elements
1 2 3 4 5 6 7 8
classSolution(object): defremoveElement(self, nums, val): index=0 for i inrange(len(nums)): if nums[i]!=val: nums[index]=nums[i] index+=1 return index
(28) Find the Index of the First Occurrence in a String
1 2 3 4 5 6
classSolution(object): defstrStr(self, haystack, needle): for i inrange(len(haystack)): if haystack[i:i+len(needle)]==needle: return i return -1