Open hon9g opened 5 years ago
i
번째 비트 삭제
def clearBitsthoughI(num: int, i: int) -> int:
mask = (1 << i) -1
return num & mask
i
부터 1의자리까지 비트 모두 삭제
def clearBitsFromIthough0(num: int, i: int) -> int:
mask = (1 << i) -1
return num & ~mask
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
n = len(nums)
result = []
for i in range(2**n):
mask = str(bin(i))[2:]
m = len(mask)
if m < n:
mask = '0'*(n-m) + mask
sub = []
for j in range(n):
if mask[j] == '1':
sub.append(nums[j])
result.append(sub)
return result
class Solution:
def singleNumber(self, nums: List[int]) -> int:
n = 0
for num in nums:
n ^= num
return n
class Solution:
def majorityElement(self, nums: List[int]) -> int:
nums = sorted(nums)
cnt, half, prev = 0, len(nums)//2, nums[0]
for num in nums:
if prev != num:
cnt = 0
cnt += 1
prev = num
if half < cnt:
break
return prev
Time Limit Exceeded
at input like m, n = 20000, 2147483647
class Solution:
def rangeBitwiseAnd(self, m: int, n: int) -> int:
if m == 0:
return 0
for num in range(m+1, n+1):
m &= num
return m
class Solution:
def rangeBitwiseAnd(self, m: int, n: int) -> int:
if len(bin(m)) != len(bin(n)):
return 0
for num in range (m+1, n+1):
m &= num
return m
modulabs cpps study ✔️ Problems from LeetCode 🌐