作者DJYOMIYAHINA (通通打死)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Sat Nov 9 12:02:53 2024
x位數是1的地方
所有elements and完一定要是1
所以這些地方一定要是1
從剩下0的地方開始填 填到n-1
蠻有趣的
不過我好像快被python寵壞
def minEnd(self, n: int, x: int) -> int:
ans = ''
n-=1
while x>0 or n>0:
if (x&1)==1:
ans = '1'+ans
else:
ans = str((n&1)) + ans
n = n >> 1
x = x >> 1
return int(ans,2)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1731124975.A.816.html
※ 編輯: DJYOMIYAHINA (125.229.37.69 臺灣), 11/09/2024 12:11:31
推 Furina: 大師 11/09 12:17
推 Meaverzt: 大師 11/09 12:24
推 sustainer123: 我已經被python寵壞了 11/09 12:31
推 dont: 大師 11/09 19:35