1. 程式人生 > >645. Set Mismatch挑出不匹配的元素和應該真正存在的元素

645. Set Mismatch挑出不匹配的元素和應該真正存在的元素

總結 優化 present turn space oss exit hset RR

[抄題]:

he set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.

Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

Example 1:

Input: nums = [1,2,2,4]
Output: [2,3]

[暴力解法]:

hashset

時間分析:

空間分析:n

[優化後]:

時間分析:

空間分析:1

[奇葩輸出條件]:

[奇葩corner case]:

[思維問題]:

[一句話思路]:

為了優化空間,判斷重復性時 直接*(-1

[輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況(不合法不合理的輸入):

[畫圖]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分鐘肉眼debug的結果]:

[總結]:

[復雜度]:Time complexity: O() Space complexity: O()

[英文數據結構或算法,為什麽不用別的數據結構或算法]:

[關鍵模板化代碼]:

[其他解法]:

[Follow Up]:

[LC給出的題目變變變]:

[代碼風格] :

645. Set Mismatch挑出不匹配的元素和應該真正存在的元素