[再エンコード]シングル番号
https://leetcode.com/problems/single-number/
1.完全なコード
1.xORプール
例えば、
2回使用した
1.完全なコード
1.xORプール
class Solution:
def singleNumber(self, nums: List[int]) -> int:
result = 0
for num in nums:
result = result ^ num
return result
2.資料型解答class Solution:
def singleNumber(self, nums: List[int]) -> int:
d = defaultdict(int)
for num in nums:
d[num] += 1
for i in d:
if d[i] == 1:
return i
2.後期first = 0
a = first ^ 70 #출력 70
b = a ^ 34 #출력 100
c = b ^ 70 #출력 34
d = c ^ 20 #출력 54
e = d ^ 34 #출력 20
2回のXOR演算を使用した70と34は初期化されていますが、1回のXOR演算を使用した20だけが値を保持します.重要なのは、同じ数が現れる順番は関係ありません.例えば、
13^512^7^512^9^7^13
のような複雑な演算も簡単に答えを得ることができる.2回使用した
13
、512
、7
は消滅し、9
万のみが残った.Reference
この問題について([再エンコード]シングル番号), 我々は、より多くの情報をここで見つけました https://velog.io/@legowww/리트코드-Single-Numberテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol