• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Uriel's Corner

            Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
            posts - 0, comments - 50, trackbacks - 0, articles - 594
            一列1-n的數(shù),其中有一個重復的(意味著有一個數(shù)missing),找出重復了一次的數(shù)以及缺少的那個數(shù)

            思路一:開個dict存每個數(shù)出現(xiàn)幾次,再掃一遍找出duplicate和missing
             1 #645
             2 #Runtime: 447 ms
             3 #Memory Usage: 15.5 MB
             4 
             5 class Solution(object):
             6     def findErrorNums(self, nums):
             7         """
             8         :type nums: List[int]
             9         :rtype: List[int]
            10         """
            11         dict_num = {}
            12         ans = [0, 0]
            13         for i in nums:
            14             if i not in dict_num:
            15                 dict_num[i] = 1
            16             else:
            17                 dict_num[i] += 1
            18         for i in range(1, len(nums) + 1):
            19             if i not in dict_num:
            20                 ans[1] = i
            21             elif dict_num[i] > 1:
            22                 ans[0] = i
            23         return ans

            思路二(看solution得到的啟發(fā),不使用其他dict等多余存儲):第一遍掃的時候?qū)⑽挥趎ums[abs(nums[i]) - 1]的數(shù)*-1,發(fā)現(xiàn)某個數(shù)已經(jīng)是負的話說明duplicate,第二遍再掃一次,找出仍然大于0的數(shù),其對應的下標就是missing的那個
             1 #645
             2 #Runtime: 495 ms
             3 #Memory Usage: 14.2 MB
             4 
             5 class Solution(object):
             6     def findErrorNums(self, nums):
             7         """
             8         :type nums: List[int]
             9         :rtype: List[int]
            10         """
            11         ans = [0, 0]
            12         for i in range(len(nums)):
            13             if nums[abs(nums[i]) - 1] < 0:
            14                 ans[0] = abs(nums[i])
            15             else:
            16                 nums[abs(nums[i]) - 1] *= -1
            17         for i in range(len(nums)):
            18             if nums[i] > 0:
            19                 ans[1] = i + 1
            20         return ans

            思路三(看solution得到的啟發(fā),異或思想,只用一重for循環(huán),但需要一個dict):原理:a^b^b=a
             1 #645
             2 #Runtime: 471 ms
             3 #Memory Usage: 15.7 MB
             4 
             5 class Solution(object):
             6     def findErrorNums(self, nums):
             7         """
             8         :type nums: List[int]
             9         :rtype: List[int]
            10         """
            11         dict_num = {}
            12         ans = [0, 0]
            13         for i in range(len(nums)):
            14             if nums[i] not in dict_num:
            15                 dict_num[nums[i]] = 1
            16             else:
            17                 ans[0] = nums[i]
            18                 dict_num[nums[i]] += 1
            19             ans[1] ^= (i + 1)
            20             ans[1] ^= nums[i]
            21         ans[1] ^= ans[0]
            22         return ans

            思路四(看solution得到的啟發(fā),異或思想,但實際只需要三重for循環(huán),不需要額外dict),原理:a^b^b=a,找出a和b二進制最后一位出現(xiàn)不同的位置,將1-n分為兩類,掃一遍nums和1-n之后,在其中一類會出現(xiàn)a,另一類出現(xiàn)b^b^b(=b),再掃一次nums就能找到b位于哪一類
             1 #645
             2 #Runtime: 306 ms
             3 #Memory Usage: 14.8 MB
             4 
             5 class Solution(object):
             6     def findErrorNums(self, nums):
             7         """
             8         :type nums: List[int]
             9         :rtype: List[int]
            10         """
            11         ans = [0, 0]
            12         xor = 0
            13         for i in range(len(nums)):
            14             xor ^= (i + 1)
            15             xor ^= nums[i]
            16         rightmostbit = xor & ~(xor - 1)
            17         for i in range(len(nums)):
            18             if (i + 1) & rightmostbit != 0:
            19                 ans[1] ^= (i + 1)
            20             else:
            21                 ans[0] ^= (i + 1)
            22             if nums[i] & rightmostbit != 0:
            23                 ans[1] ^= nums[i]
            24             else:
            25                 ans[0] ^= nums[i]
            26         for i in nums:
            27             if ans[0] == i:
            28                 return ans
            29         return [ans[1], ans[0]]
            国内精品久久久久久野外| 亚洲∧v久久久无码精品| 久久99精品九九九久久婷婷| 久久久久久无码国产精品中文字幕 | 少妇熟女久久综合网色欲| 午夜天堂精品久久久久| 久久久精品国产亚洲成人满18免费网站| 久久综合色之久久综合| 成人综合伊人五月婷久久| 久久婷婷色香五月综合激情| 久久国产乱子伦免费精品| 久久精品国产清自在天天线| 国产成人精品免费久久久久| 国内精品久久久久影院亚洲| 国产精品免费久久| 少妇久久久久久久久久| 亚洲国产精品无码久久九九| 久久噜噜电影你懂的| 无码人妻精品一区二区三区久久| 久久精品人妻一区二区三区| 久久亚洲精品国产精品| 久久经典免费视频| 久久久久亚洲AV成人网人人网站 | 一级做a爱片久久毛片| 亚洲日韩中文无码久久| 亚洲精品国精品久久99热| 香蕉久久夜色精品国产小说| 久久久久久九九99精品| 久久精品国产亚洲AV蜜臀色欲| 久久精品女人天堂AV麻| 久久精品国产一区二区三区不卡 | 久久久久这里只有精品 | 久久久中文字幕| 狠狠久久亚洲欧美专区| 国产亚洲精品自在久久| 久久精品国产亚洲av麻豆色欲 | 国产高清美女一级a毛片久久w| 狠狠色丁香久久婷婷综合五月| 亚洲色大成网站www久久九 | 久久久青草久久久青草| 色综合久久综合网观看|