Day5

  1. 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

  1. Implement strStr()
    **思路:天哪,做出了兩道了。哈哈哈
    不過(guò)這個(gè)很簡(jiǎn)單,就是字符串的比較。
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 

  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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容