This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| public int majorityElement(int[] nums) { | |
| if(nums == null || nums.length == 0) return -1; | |
| int ret = nums[0], count=1; | |
| for(int i=1; i<nums.length; i++){ | |
| if(count == 0){ | |
| ret = nums[i]; | |
| count = 1; | |
| } | |
| else if(ret == nums[i]){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| public int trailingZeroes(int n) { | |
| int ret = 0; | |
| while(n != 0){ | |
| ret += n/5; | |
| n = n/5; | |
| } | |
| return ret; | |
| } | |
| // |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| public String largestNumber(int[] nums) { | |
| if(nums == null || nums.length==0) return null; | |
| String str = ""; | |
| ArrayList<Integer> list = new ArrayList<>(); | |
| for(int num : nums){ | |
| list.add(num); | |
| } | |
| Collections.sort(list, comparator); | |
| for(int i=0; i<list.size(); i++){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // better solution | |
| public int reverseBits(int n) { | |
| int ret = 0; | |
| for(int i=0; i<32; i++){ | |
| ret = (ret << 1) + (n & 1); | |
| n >>=1; | |
| } | |
| return ret; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int hammingWeight(int n) { | |
| int ret = 0; | |
| while(n != 0){ | |
| n &= n-1; | |
| ret++; | |
| } | |
| return ret; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| public List<Integer> rightSideView(TreeNode root) { | |
| List<Integer> ret = new ArrayList<Integer>(); | |
| helper(ret, root, 0); | |
| return ret; | |
| } | |
| private void helper(List<Integer> ret, TreeNode root, int level){ | |
| if(root == null) return; | |
| if(ret.isEmpty() || level >= ret.size()){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // BFS ---> note: this my cause issue when you encode the coordinates | |
| public int numIslands(char[][] grid) { | |
| if(grid==null || grid.length==0) return 0; | |
| int count = 0; | |
| for(int i=0; i<grid.length; i++){ | |
| for(int j=0; j<grid[0].length; j++){ | |
| if(grid[i][j] == '1'){ | |
| count++; | |
| setZeros(grid, i, j); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| public int rangeBitwiseAnd(int m, int n) { | |
| while(m<n) n = n & (n-1); | |
| return n; | |
| } | |
| // | |
| public int rangeBitwiseAnd(int m, int n) { | |
| return (n > m) ? (rangeBitwiseAnd(m/2, n/2) << 1) : m; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // O(1) space complexity | |
| int digitSquareSum(int n) { | |
| int sum = 0, tmp; | |
| while (n) { | |
| tmp = n % 10; | |
| sum += tmp * tmp; | |
| n /= 10; | |
| } | |
| return sum; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public ListNode removeElements(ListNode head, int val) { | |
| if(head == null) return head; | |
| ListNode dummyNode = new ListNode(0); | |
| dummyNode.next = head; | |
| ListNode pre = dummyNode; | |
| while(pre.next != null){ | |
| ListNode cur = pre.next; | |
| if(cur.val == val){ | |
| pre.next = pre.next.next; |