Day5

作者: wendy_要努力努力再努力 | 来源:发表于2017-11-03 11:05 被阅读0次
    1. Remove Element
      **思路;这个问题和昨天的移除相同元素问题类似,纪念人生第一次做出一道easy题,哈哈哈 终于看到了一点进步。还是让nums[j]在for循环和if条件下自己生成一个新的符合条件的数组,数组保证是子数组,所以不用开辟新的空间。
    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()
      **思路:天哪,做出了两道了。哈哈哈
      不过这个很简单,就是字符串的比较。
    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
      **思路:这个也很简单,因为数组是排好序的,不用考虑二分等等方法,只要顺序遍历,找到合适的位置插入便可以。
    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)
    

    相关文章

      网友评论

          本文标题:Day5

          本文链接:https://www.haomeiwen.com/subject/ypazpxtx.html