原题链接:
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
https://leetcode.com/problems/remove-element
https://leetcode.com/problems/implement-strstr
这几个比较容易,就直接记录一下代码了。
Remove Duplicates from Sorted Array
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i = 0
count = 0
while i < len(nums)-1:
while nums[i] == nums[i+1]:
del nums[i + 1]
if i == len(nums)-1:
break
i += 1
return len(nums)
Remove Element
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
while val in nums:
nums.remove(val)
return len(nums)
Implement strStr()
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if not needle or haystack==needle:
return 0
i = 0
while i <= len(haystack) - len(needle):
if haystack[i] == needle[0]:
if haystack[i:i + len(needle)] == needle:
return i
i += 1
return -1