Remove Element
**思路;這個(gè)問(wèn)題和昨天的移除相同元素問(wèn)題類似,紀(jì)念人生第一次做出一道easy題,哈哈哈 終于看到了一點(diǎn)進(jìn)步。還是讓nums[j]在for循環(huán)和if條件下自己生成一個(gè)新的符合條件的數(shù)組,數(shù)組保證是子數(shù)組,所以不用開(kāi)辟新的空間。
lass Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
j=0
for i in range(len(nums)):
if nums[i] != val:
nums[j]=nums[i]
j=j+1
return j
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
a = len(needle)
for i in range(len(haystack)-a+1):
if haystack[i:i+a] == needle:
return i
return -1
Search Insert Position
**思路:這個(gè)也很簡(jiǎn)單,因?yàn)閿?shù)組是排好序的,不用考慮二分等等方法,只要順序遍歷,找到合適的位置插入便可以。
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
for i in range(len(nums)):
if target <= nums[i]:
return i
return len(nums)