GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/868_Binary_Gap.py at master · qiyuangong/leetcode Expand file tree/ Copy path868_Binary_Gap.pyMore file actions 34 lines (30 loc) · 961 Bytes / Copy path868_Binary_Gap.pyFile metadata and controls34 lines (30 loc) · 961 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 class Solution: # def binaryGap(self, n: int) -> int: # # Store index # A = [i for i in xrange(32) if (N >> i) & 1] # if len(A) < 2: return 0 # return max(A[i+1] - A[i] for i in xrange(len(A) - 1)) def binaryGap(self, n: int) -> int: # one pass and store max current = 1 last1 = -1 out = 0 while n > 0: if n % 2 == 1: if last1 >= 1: out = max(out, current - last1) last1 = current current += 1 n = n // 2 return out # def binaryGap(self, n: int) -> int: # # one pass and store max # res = 0 # last = -1 # # Get binary encoding with bin # for i, curr in enumerate(bin(n)[2:]): # if curr == '1': # if last >= 0: # res = max(res, i - last) # last = i # return res |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |