• <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ù),其中有一個(gè)重復(fù)的(意味著有一個(gè)數(shù)missing),找出重復(fù)了一次的數(shù)以及缺少的那個(gè)數(shù)

            思路一:開(kāi)個(gè)dict存每個(gè)數(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等多余存儲(chǔ)):第一遍掃的時(shí)候?qū)⑽挥趎ums[abs(nums[i]) - 1]的數(shù)*-1,發(fā)現(xiàn)某個(gè)數(shù)已經(jīng)是負(fù)的話說(shuō)明duplicate,第二遍再掃一次,找出仍然大于0的數(shù),其對(duì)應(yīng)的下標(biāo)就是missing的那個(gè)
             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),但需要一個(gè)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ā),異或思想,但實(shí)際只需要三重for循環(huán),不需要額外dict),原理:a^b^b=a,找出a和b二進(jìn)制最后一位出現(xiàn)不同的位置,將1-n分為兩類,掃一遍nums和1-n之后,在其中一類會(huì)出現(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]]
            青青青青久久精品国产| 久久亚洲精品国产亚洲老地址| 精品久久久久久无码不卡| 狠狠色婷婷综合天天久久丁香| 久久精品午夜一区二区福利| 国产成人精品三上悠亚久久| 久久久91人妻无码精品蜜桃HD| 精品国产一区二区三区久久| 久久成人国产精品免费软件| 亚洲七七久久精品中文国产| 2021少妇久久久久久久久久| 爱做久久久久久| 精品伊人久久久| 久久综合狠狠综合久久激情 | 日本三级久久网| 久久精品人人做人人爽97| 久久成人小视频| 国内精品九九久久精品| 久久伊人中文无码| 色婷婷狠狠久久综合五月| 久久国产免费直播| 国产69精品久久久久777| 国产色综合久久无码有码| 久久综合给久久狠狠97色| 久久久久亚洲精品天堂| 日产精品99久久久久久| 日韩精品久久久肉伦网站| 99久久超碰中文字幕伊人| 夜夜亚洲天天久久| 亚洲乱亚洲乱淫久久| 久久男人AV资源网站| 久久人做人爽一区二区三区| 久久美女人爽女人爽| 午夜精品久久久久久久无码| 中文字幕无码久久人妻| 久久AV高清无码| 久久精品国产亚洲av麻豆图片| 99久久精品国产一区二区| 97久久超碰国产精品2021| 国产精品99久久99久久久| 亚洲精品国产成人99久久|