Posted on 2023-11-16 19:49
Uriel 閱讀(41)
評論(0) 編輯 收藏 引用 所屬分類:
閑來無事重切Leet Code 、
位運算
給出n個長度為n的二進制數,輸出任何一個同樣長度為n且與這n個數都不同的二進制數,python的bin()可以方便轉二進制
#1980
#Runtime: 11 ms (Beats 91.4%)
#Memory: 13.5 MB (Beats 47.76%)
class Solution(object):
def findDifferentBinaryString(self, nums):
"""
:type nums: List[str]
:rtype: str
"""
n = len(nums)
num_set = set(int(x, 2) for x in nums)
def change_to_n_bit(x, n):
return ("0"*(n - len(x))) + x
for x in xrange(1<<n):
if x not in num_set:
return change_to_n_bit(bin(x)[2:], n)