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 class Solution { | |
| // you need to treat n as an unsigned value | |
| public int hammingWeight(int n) { | |
| int count = 0; | |
| while(n != 0){ | |
| n = n & (n-1); | |
| count++; | |
| } | |
| return count; | |
| } |
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
| /* | |
| For python: | |
| -123 % 10 = 7 | |
| -123 / 10 = -13 | |
| For Java: | |
| -123 % 10 = -3 | |
| -123 / 10 = -12 | |
| */ |
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
| //"barfoofoobarthefoobarman" ["bar","foo","the"] | |
| public class Solution { | |
| public List<Integer> findSubstring(String s, String[] words) { | |
| List<Integer> res = new ArrayList<Integer>(); | |
| if (words == null || words.length == 0 || s.length() < words.length * words[0].length()) return res; | |
| int count = words.length; | |
| int k = words[0].length(); | |
| int len = s.length(); | |
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
| class TrieNode{ | |
| private TrieNode[] children; | |
| public boolean hasWord; | |
| public TrieNode() { | |
| children = new TrieNode[26]; | |
| hasWord = false; | |
| } | |
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
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; | |
| * TreeNode(int x) { val = x; } | |
| * } | |
| */ | |
| class Solution { |
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
| /* | |
| Here I use typical BFS method to handle a binary tree. I use string n to represent null values. The string of the binary tree in the example will be "1 2 3 n n 4 5 n n n n ". | |
| When deserialize the string, I assign left and right child for each not-null node, and add the not-null children to the queue, waiting to be handled later. | |
| */ | |
| public class Codec { | |
| public String serialize(TreeNode root) { | |
| if (root == null) return ""; |
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
| /** | |
| * Definition for an interval. | |
| * public class Interval { | |
| * int start; | |
| * int end; | |
| * Interval() { start = 0; end = 0; } | |
| * Interval(int s, int e) { start = s; end = e; } | |
| * } | |
| */ | |
| public class SummaryRanges { |
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 class Solution { | |
| public int search(int[] nums, int target) { | |
| if(nums == null || nums.length == 0) return -1; | |
| int start = 0; | |
| int end = nums.length - 1; | |
| while(start <= end){ | |
| int mid = (start + end) / 2; | |
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
| class Solution { | |
| private int find(int[] parents, int i) { | |
| if (parents[i] == -1) { | |
| return i; | |
| } | |
| return find(parents, parents[i]); | |
| } | |
| public boolean validTree(int n, int[][] edges) { | |
| int[] parents = new int[n]; | |
| Arrays.fill(parents, -1); |
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
| class Solution { | |
| public boolean canFinish(int numCourses, int[][] prerequisites) { | |
| if (prerequisites == null || prerequisites.length < 1 || prerequisites[0].length < 1) return true; | |
| List<List<Integer>> adj = new ArrayList<>(); | |
| int[] indegree = new int[numCourses]; | |
| for (int i = 0; i < numCourses; i++) { | |
| adj.add(new ArrayList<>()); | |
| } | |
| for (int[] e : prerequisites) { | |
| adj.get(e[0]).add(e[1]); |