GeezerVison is showing github.com — Refine 868 and add link · pengaoao/leetcode@417ccfd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -205,6 +205,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal | 205 | 205 | | 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/844_Backspace_String_Compare.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/844_Backspace_String_Compare.java) | 1. Stack pop when encounters #, O(n) and O(n)<br>2. Compare string from end to start, O(n) and O(1) | | 206 | 206 | | 852 | [Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/852_Peak_Index_in_a_Mountain_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/852_Peak_Index_in_a_Mountain_Array.java) | 1. Scan the array until encountering decline, O(n) and O(1)<br>2. Binary seach with additional check for [i + 1], O(logn) and O(1) | | 207 | 207 | | 867 | [Transpose Matrix](https://leetcode.com/problems/transpose-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/867_Transpose_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/867_Transpose_Matrix.java) | Res[i][j] = A[j][i] | | | 208 | +| 868 | [Binary Gap](https://leetcode.com/problems/binary-gap/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/868_Binary_Gap.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/868_Binary_Gap.java) | 1. Store index and check, O(logn) and O(logn)<br>2. One pass and store max, O(logn) and O(1) | | 208 | 209 | | 872 | [Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/872_Leaf-Similar_Trees.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/872_Leaf-Similar_Trees.java) | DFS (stack or recursion) get leaf value sequence and compare, O(n) and O(n) | | 209 | 210 | | 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/876_Middle_of_the_Linked_List.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/876_Middle_of_the_Linked_List.java) | 1. Copy to array, O(n) and O(n)<br>2. Fast and slow point, where fast point is 2 times faster than slow point, O(n) and O(1) | | 210 | 211 | | 904 | [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/904_Fruit_Into_Baskets.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/904_Fruit_Into_Baskets.java) | 1. Scan through blocks of tree, O(n) and O(n)<br>2. Mainten a sliding window with start and curr point, O(n) and O(n). | | |
Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +class Solution { | | 2 | + /*public int binaryGap(int n) { | | 3 | + // Store Indexes | | 4 | + int[] A = new int[32]; | | 5 | + int t = 0; | | 6 | + for (int i = 0; i < 32; ++i) | | 7 | + if (((N >> i) & 1) != 0) | | 8 | + A[t++] = i; | | 9 | + | | 10 | + int ans = 0; | | 11 | + for (int i = 0; i < t - 1; ++i) | | 12 | + ans = Math.max(ans, A[i+1] - A[i]); | | 13 | + return ans; | | 14 | + }*/ | | 15 | + | | 16 | + public int binaryGap(int N) { | | 17 | + int last = -1, ans = 0; | | 18 | + for (int i = 0; i < 32; ++i) | | 19 | + if (((N >> i) & 1) > 0) { | | 20 | + // Store max | | 21 | + if (last >= 0) | | 22 | + ans = Math.max(ans, i - last); | | 23 | + last = i; | | 24 | + } | | 25 | + return ans; | | 26 | + } | | 27 | +} |
Collapse file Copy file name to clipboard +25-5Lines changed: 25 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | class Solution: | | 2 | + | | 3 | + # def binaryGap(self, n: int) -> int: | | 4 | + # # Store index | | 5 | + # A = [i for i in xrange(32) if (N >> i) & 1] | | 6 | + # if len(A) < 2: return 0 | | 7 | + # return max(A[i+1] - A[i] for i in xrange(len(A) - 1)) | | 8 | + | | 9 | + | 2 | 10 | def binaryGap(self, n: int) -> int: | 3 | | - current= 1 | | 11 | + # one pass and store max | | 12 | + current = 1 | 4 | 13 | last1 = -1 | 5 | 14 | out = 0 | 6 | 15 | while n > 0: | 7 | | - if n%2 == 1: | | 16 | + if n % 2 == 1: | 8 | 17 | if last1 >= 1: | 9 | 18 | out = max(out, current - last1) | 10 | 19 | last1 = current | 11 | 20 | current += 1 | 12 | | - n = n//2 | 13 | | - return(out) | 14 | | - | | 21 | + n = n // 2 | | 22 | + return out | | 23 | + | | 24 | + # def binaryGap(self, n: int) -> int: | | 25 | + # # one pass and store max | | 26 | + # res = 0 | | 27 | + # last = -1 | | 28 | + # # Get binary encoding with bin | | 29 | + # for i, curr in enumerate(bin(n)[2:]): | | 30 | + # if curr == '1': | | 31 | + # if last >= 0: | | 32 | + # res = max(res, i - last) | | 33 | + # last = i | | 34 | + # return res |
|