11 2013 档案
摘要:1 public class Solution { 2 public int[] twoSum(int[] numbers, int target) { 3 HashMap visited = new HashMap(); 4 int[] result = new int[2]; 5 for(int i = 0; i < numbers.length; i++){ 6 int tmp = target - numbers[i]; 7 if(visited.containsKey(tmp))...
阅读全文
摘要:1 public class Solution { 2 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 3 if(l1 == null) 4 return l2; 5 if(l2 == null) 6 return l1; 7 ListNode fakehead = new ListNode(0); 8 ListNode pre = fakehead; 9 int carry = 0;10 ...
阅读全文
摘要:1 public class Solution { 2 public int longestConsecutive(int[] num) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int len = num.length; 6 if(len table = new HashMap(); 9 ...
阅读全文
摘要:String equals() == 1 public class Solution { 2 public int evalRPN(String[] tokens) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 Stack stack = new Stack(); 6 if(tokens == null |...
阅读全文
摘要:1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */10 public class Solution {11 public int minDepth(TreeNode root) {12 // IMPORTANT: Please reset any memb...
阅读全文
摘要:1 public class Solution { 2 public int singleNumber(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int len = A.length; 6 if(A == null) 7 return 0; 8 ...
阅读全文
摘要:1 public class Solution { 2 public int maxProfit(int[] prices) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(prices == null||prices.length == 0) 6 return 0; 7 in...
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public class Solution {13 public boolean hasCycle(ListNode head) {14 // IMPO...
阅读全文
摘要:bit manipulation 1 public class Solution { 2 public int singleNumber(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(A == null || A.length == 0) 6 return 0; 7 ...
阅读全文
摘要:be careful when x1 = x2 1 /** 2 * Definition for a point. 3 * class Point { 4 * int x; 5 * int y; 6 * Point() { x = 0; y = 0; } 7 * Point(int a, int b) { x = a; y = b; } 8 * } 9 */10 public class Solution {11 public int maxPoints(Point[] points) {12 // IMPORTANT: ...
阅读全文
摘要:Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. example duck application same super class, same function name, different function implementationstructure:
阅读全文
摘要:1 public class LRUCache { 2 3 private int capacity; 4 private Map nodes; 5 private int currentSize; 6 private Entry first; 7 private Entry last; 8 9 public LRUCache(int capacity) {10 this.capacity = capacity;11 currentSize = 0;12 nodes = new...
阅读全文
摘要:1 public class Solution { 2 public ArrayList wordBreak(String s, Set dict) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 if (s == null || ...
阅读全文
摘要:1 public class Solution { 2 public ArrayList fullJustify(String[] words, int L) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int wordsCount = words.length; 6 ArrayList result ...
阅读全文
摘要:1 public class Solution { 2 public double findMedianSortedArrays(int A[], int B[]) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int aLen = A.length; 6 int bLen = B.length; 7 ...
阅读全文
摘要:1 public class Solution { 2 public ArrayList findSubstring(String S, String[] L) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList results = new ArrayList(); 6 int len = ...
阅读全文
摘要:1 public class Solution { 2 public boolean isNumber(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(s==null) 6 return false; 7 char[] sArr = s.trim().toCharArray...
阅读全文
摘要:1 public class Solution { 2 public String minWindow(String S, String T) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if (S == null || T == null || S.length() == 0 || T.length() == 0) ...
阅读全文
摘要:1 public class Solution { 2 public boolean isMatch(String s, String p) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int sLen = s.length(), pLen = p.length(); 6 int i = 0, j = ...
阅读全文
摘要:1 public class Solution { 2 public boolean isInterleave(String s1, String s2, String s3) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int len1 = s1.length(), len2 = s2.length(), len3 =...
阅读全文
摘要:1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */10 public class Solution {11 public int maxPathSum(TreeNode root) {12 ArrayList maxSum = new ArrayList(...
阅读全文
摘要:1 public class Solution { 2 public boolean isMatch(String s, String p) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(p == null||p.length() == 0) return s == null||s.length()==0; 6 ...
阅读全文
摘要:1 public class Solution { 2 public ArrayList merge(ArrayList intervals) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 if(intervals == null...
阅读全文
摘要:1 public class Solution { 2 public boolean isScramble(String s1, String s2) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int n=s1.length(); 6 boolean[][][] dp=new boolean[n][n...
阅读全文
摘要:1 public class Solution { 2 public ArrayList spiralOrder(int[][] matrix) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 if(matrix == null||...
阅读全文
摘要:1 public class Solution { 2 public void solveSudoku(char[][] board) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 solveSudokuRecursive(board); 6 } 7 private boolean solveSudoku...
阅读全文
摘要:1 public class Solution { 2 public static ArrayList restoreIpAddresses(String s) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 ArrayList result = new ArrayList(); 6 if (s == null || s.length() == 0) { 7 return result...
阅读全文
摘要:1 public class Solution { 2 public String strStr(String haystack, String needle) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int needleLen = needle.length(); 6 int haystackLe...
阅读全文
摘要:1 public class Solution { 2 public ArrayList insert(ArrayList intervals, Interval newInterval) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 ...
阅读全文
摘要:threaded binary tree 1 public class Solution { 2 public void recoverTree(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 TreeNode f1 = null, f2 = null; 6 TreeNode ...
阅读全文
摘要:1 public class Solution { 2 public int firstMissingPositive(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int len = A.length; 6 for (int i = 0; i 0 && A[i] != i + 1 &...
阅读全文
摘要:1 public class Solution { 2 public ListNode rotateRight(ListNode head, int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(head == null || n == 0){ 6 return head; 7 ...
阅读全文
摘要:1 public class Solution { 2 public String longestPalindrome(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int n = s.length(); 6 if (n == 0) return ""; 7 Strin...
阅读全文
摘要:similar with 3Sum. O(n3) 1 public class Solution { 2 public ArrayList> fourSum(int[] num, int target) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 Arrays.sort(num); 6 HashSet> ...
阅读全文
摘要:1 public class Solution { 2 public ArrayList anagrams(String[] strs) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 int len = strs.length; 6 ArrayList result = new ArrayList(); 7 Map> sorted = new HashMap>(); 8 9...
阅读全文
摘要:1 public class Solution { 2 public ListNode reverseKGroup(ListNode head, int k) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 6 if(head == null || k == 1) 7 return head; 8 9 int len = 0;10 ...
阅读全文
摘要:1 public class Solution { 2 public int jump(int[] A) { 3 if(A == null){ 4 return 0; 5 } 6 int len = A.length; 7 if(len == 0 || len == 1){ 8 return 0; 9 }10 11 int cur = 0;12 int next = 0;13 int ret =...
阅读全文
摘要:1 public class Solution { 2 public ArrayList> combinationSum2(int[] num, int target) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 HashSet> result = new HashSet>(); 6 ArrayList...
阅读全文
摘要:1 public class Solution { 2 public int totalNQueens(int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 int depth = 0; 7 int[] ro...
阅读全文
摘要:1 public class Solution { 2 public static ArrayList solveNQueens(int n) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 ArrayList result = new ArrayList(); 6 int depth = 0; 7 int[] rows = new int[n]; 8 for (int i =...
阅读全文
摘要:1 public class Solution { 2 public ListNode insertionSortList(ListNode head) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ListNode prehead = new ListNode(0); 6 ListNode runner...
阅读全文
摘要:1 public class Solution { 2 public ArrayList> combinationSum(int[] candidates, int target) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 ArrayList> result = new ArrayList>(); 6 int len = candidates.length, depth = 0; 7 i...
阅读全文
摘要:1 public class Solution { 2 public void nextPermutation(int[] num) { 3 // Note: The Solution object is instantiated only once and is reused by each test case. 4 int length = num.length; 5 int index = - 1; 6 for(int i = length - 1; i >= 1; i--){ 7 if(n...
阅读全文
摘要:1 public class Solution { 2 public ArrayList> permuteUnique(int[] num) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int len = num.length; 6 ArrayList> result = new ArrayList>(...
阅读全文
摘要:1 public class Solution { 2 public int uniquePathsWithObstacles(int[][] obstacleGrid) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int m = obstacleGrid.length; 6 int n = obsta...
阅读全文
摘要:1 public class Solution { 2 public ArrayList> permute(int[] num) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int len = num.length; 6 ArrayList> result = new ArrayList>(); 7 ...
阅读全文
摘要:1 public class Solution { 2 public int minPathSum(int[][] grid) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int m = grid.length; 6 int n = grid[0].length; 7 int[][] r...
阅读全文
摘要:1 public class Solution { 2 public int maxSubArray(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int result = Integer.MIN_VALUE; 6 int curSum = 0; 7 for(int i ...
阅读全文
摘要:special牛顿迭代法 1 public class Solution { 2 public int sqrt(int x) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(x 0.0001){11 last = cur;12 cur = (cur + x / cur...
阅读全文
摘要:all the possible 1 public class Solution { 2 public String simplifyPath(String path) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 Stack mystack = new Stack(); 6 char[] mystring...
阅读全文
摘要:binary search 1 public class Solution { 2 public int[] searchRange(int[] A, int target) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int[] result = new int[2]; 6 if(A == null){...
阅读全文
摘要:1 public class Solution { 2 public int minDistance(String word1, String word2) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int m = word1.length(), n = word2.length(); 6 int[]...
阅读全文
摘要:public class Solution { public void setZeroes(int[][] matrix) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(matrix == null || matrix.length == 0) return; boolean column = fal...
阅读全文
摘要:DFS 1 public class Solution { 2 public boolean exist(char[][] board, String word) { 3 if(word == null || word.length() done = new HashSet(); 6 7 for(int i = 0; i done){17 if(row = board.length || column >= board[0].length)18 return false;19 ...
阅读全文
摘要:1 public class Solution { 2 public ListNode deleteDuplicates(ListNode head) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(head == null || head.next == null) 6 return hea...
阅读全文
摘要:dynamic programming 1 public class Solution { 2 public int maximalRectangle(char[][] matrix) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(matrix == null||matrix.length == 0) 6 ...
阅读全文
摘要:1 public class Solution { 2 public ListNode partition(ListNode head, int x) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ListNode fakeLeftHead = new ListNode(0); 6 ListNode fa...
阅读全文
摘要:recursion 1 public class Solution { 2 public ArrayList> combine(int n, int k) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList> result = new ArrayList>(); 6 if(n list = ...
阅读全文
摘要:判断左右哪一半sorted对那一边进行binary search 1 public class Solution { 2 public boolean search(int[] A, int target) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(A == null) 6 return ...
阅读全文
摘要:1 public class Solution { 2 public int search(int[] A, int target) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(A == null) 6 return -1; 7 int left = 0; 8 ...
阅读全文
摘要:1 public class Solution { 2 public ArrayList grayCode(int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 if(n >1));11 return res...
阅读全文
摘要:dynamic programming 1 public class Solution { 2 public int numDecodings(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int length = s.length(); 6 if(0 == length || s.ch...
阅读全文
摘要:1 public class Solution { 2 public ArrayList> subsetsWithDup(int[] num) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList> result = new ArrayList>(); 6 ArrayList list = n...
阅读全文
摘要:1 public class Solution { 2 public ArrayList> subsets(int[] S) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList> result = new ArrayList>(); 6 ArrayList list = new ArrayL...
阅读全文
摘要:1 public class Solution { 2 public ListNode reverseBetween(ListNode head, int m, int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ListNode newhead = new ListNode(0); 6 7 ...
阅读全文
摘要:1 public class Solution { 2 public ArrayList inorderTraversal(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 traversal(root,...
阅读全文
摘要:1 public class Solution { 2 public ArrayList generateTrees(int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 return generate(1, n); 6 } 7 public ArrayList generate(int start...
阅读全文
摘要:1 public class Solution { 2 public boolean isValidBST(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(root == null) 6 return true; 7 if(root.left !=...
阅读全文
摘要:BFS 1 public class Solution { 2 public ArrayList> zigzagLevelOrder(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 LinkedList visiting = new LinkedList(); 6 LinkedL...
阅读全文
摘要:1 public class Solution { 2 public TreeNode buildTree(int[] inorder, int[] postorder) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(inorder == null||postorder == null||inorder.lengt...
阅读全文
摘要:1 public class Solution { 2 public ArrayList> levelOrderBottom(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList> result = new ArrayList>(); 6 if(root == n...
阅读全文
摘要:1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */10 public class Solution {11 public TreeNode sortedArrayToBST(int[] num) {12 // IMPORTANT: Please reset...
阅读全文
摘要:1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */10 public class Solution {11 public boolean isBalanced(TreeNode root) {12 // IMPORTANT: Please reset an...
阅读全文
摘要:1 public class Solution { 2 public ArrayList> pathSum(TreeNode root, int sum) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList list = new ArrayList(); 6 ArrayList> resul...
阅读全文
摘要:1 public class Solution { 2 public boolean hasPathSum(TreeNode root, int sum) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(root == null) 6 return false; 7 boole...
阅读全文
摘要:将左子树移到右边循环即可 1 public class Solution { 2 public void flatten(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 while(root != null){ 6 if(root.left != null){ 7 ...
阅读全文
摘要:1 public class Solution { 2 public int numDistinct(String S, String T) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(S == null || S.length() == 0){ 6 return 0; 7 ...
阅读全文
摘要:先遍历右子树 1 public class Solution { 2 public void connect(TreeLinkNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(root == null) 6 return; 7 8 T...
阅读全文
摘要:recursion programming画图看特点 1 public class Solution { 2 public void connect(TreeLinkNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(root == null) 6 return; 7 ...
阅读全文
摘要:1 public class Solution { 2 public ArrayList getRow(int rowIndex) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList previous = new ArrayList(); 6 ArrayList result = new A...
阅读全文
摘要:1 public class Solution { 2 public ArrayList> generate(int numRows) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList> result = new ArrayList>(); 6 if(numRows == 0) 7 ...
阅读全文
摘要:1 public class Solution { 2 public int minimumTotal(ArrayList> triangle) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int length = triangle.size(); 6 int[] result = new int[le...
阅读全文
摘要:前后两遍遍历,算出当前位置之前和之后的最大利润。 1 public class Solution { 2 public int maxProfit(int[] prices) { 3 4 if(prices == null || prices.length == 0) 5 return 0; 6 7 int[] max = new int[prices.length]; 8 int min = prices[0]; 9 int result = 0;10 ...
阅读全文
摘要:注意各种数据结构的特点,删除的时候要注意遍历 1 public class Solution { 2 public int ladderLength(String start, String end, HashSet dict) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 6 LinkedList qu...
阅读全文
摘要:1 public class Solution { 2 public void solve(char[][] board) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(board == null||board.length == 0||board[0] == null||board[0].length == 0)...
阅读全文
摘要:dynamic programming 1 public class Solution { 2 public static int minCut(String s) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 if (s == null) { 6 return 0; 7 } 8 int len = s.length(); 9 int[] cuts = ...
阅读全文
摘要:注意add arraylist 到 ArrayList>的时候传递引用和传值得区别!!! 1 public class Solution { 2 public ArrayList> partition(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList> result = new Arra...
阅读全文
摘要:BFS 用hashmap记录对应关系 1 public class Solution { 2 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(node == null) 6 ...
阅读全文
摘要:dynamic programming 1 public class Solution { 2 public boolean wordBreak(String s, Set dict) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(s == null||dict.isEmpty()) 6 re...
阅读全文
摘要:只有一个答案,找到最后一个不可以的点,下一个是开始点 1 public class Solution { 2 public int canCompleteCircuit(int[] gas, int[] cost) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int total = 0; 6 int su...
阅读全文
摘要:两遍遍历,使得左右邻居都合法 1 public class Solution { 2 public int candy(int[] ratings) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int[] candy = new int[ratings.length]; 6 int count = 0; ...
阅读全文
摘要:fastrunner and slowrunner trick 1 public class Solution { 2 public ListNode detectCycle(ListNode head) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ListNode fastrunner = head; 6 ...
阅读全文
摘要:将midnode后面的node的指针反转,再从头尾两边同时遍历注意midnode的next设为null 1 public class Solution { 2 public void reorderList(ListNode head) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int count = 0; 6 ...
阅读全文
摘要:recursion 1 public class Solution { 2 public ArrayList preorderTraversal(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 trave...
阅读全文
摘要:recursion programming 1 public class Solution { 2 public TreeNode sortedListToBST(ListNode head) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(head == null) 6 return null...
阅读全文
摘要:从后向前遍历 1 public class Solution { 2 public void merge(int A[], int m, int B[], int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int indexA = m - 1; 6 int indexB = n - 1; 7 ...
阅读全文
摘要:用双index从头尾两方向同时遍历 1 public class Solution { 2 public void sortColors(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int frontRunner = 0; 6 int backRunner = A.length - 1;...
阅读全文
摘要:recursion programming 1 public class Solution { 2 public TreeNode buildTree(int[] preorder, int[] inorder) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(preorder == null) 6 ...
阅读全文
摘要:recursion programming 1 public class Solution { 2 public ArrayList postorderTraversal(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 ...
阅读全文
摘要:recursion programming 1 public class Solution { 2 public boolean isSymmetric(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(root == null) 6 return true; 7 ...
阅读全文
摘要:先按照对角线翻转,再按照x轴翻转 1 public class Solution { 2 public void rotate(int[][] matrix) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int len = matrix.length; 6 if(len == 0) 7 ...
阅读全文
摘要:注意边界 1 public class Solution { 2 public int[][] generateMatrix(int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(n = end)14 return;15 if(end - start == 1){16 ...
阅读全文
摘要:1 public class Solution { 2 public boolean isPalindrome(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(s == null || s.length() < 2) 6 return true; 7 Str...
阅读全文
摘要:profit 可以累积 1 public class Solution { 2 public int maxProfit(int[] prices) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(prices == null) 6 return 0; 7 int result ...
阅读全文
摘要:bfs 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */10 public class Solution {11 public ArrayList> levelOrder(TreeNode root) {12 // IMPORTANT: Please re...
阅读全文
摘要:找出规律 1 public class Solution { 2 public String getPermutation(int n, int k) { 3 int total = 1; 4 ArrayList rest = new ArrayList(); 5 StringBuilder result = new StringBuilder(); 6 int m = n; 7 8 while(m > 1){ 9 total*=m;10 m...
阅读全文
摘要:similar with Trapping rain water 1 public class Solution { 2 public int largestRectangleArea(int[] height) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int result = 0; 6 for(in...
阅读全文
摘要:1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */10 public class Solution {11 public int sumNumbers(TreeNode root) {12 // IMPORTANT: Please reset any me...
阅读全文
摘要:用maxstep记录最大步程,然后遍历(greedy) 1 public class Solution { 2 public boolean canJump(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int maxstep = 0; 6 for(int i = 0; i maxste...
阅读全文
摘要:直接计算超时二分法 O(lgn) 1 public class Solution { 2 public double pow(double x, int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(n == 0) 6 return 1; 7 if(n == 1) 8 ...
阅读全文
摘要:每个位置的水量 = 左右最大值中较小的一个 - 这个位置的高度 1 public class Solution { 2 public int trap(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(A == null) 6 return 0; 7 int re...
阅读全文
摘要:大数相乘, 按位算结果, 注意全0,注意进位 1 public class Solution { 2 public String multiply(String num1, String num2) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int length1 = num1.length(); 6 ...
阅读全文
摘要:simple recursion problem 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */10 public class Solution {11 public boolean isSameTree(TreeNode p, TreeNode q) {12 ...
阅读全文
摘要:slowrunner and fastrunner trick 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public class Solution {13 public ListNode dele...
阅读全文
摘要:similar withRemove Duplicates from Sorted Array 1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(A == null) 6 ...
阅读全文
摘要:simple recursion problem 1 public class Solution { 2 public int climbStairs(int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int[] table = new int[n+1]; 6 for(int i = 0; i <...
阅读全文
摘要:use stack 1 public class Solution { 2 public String addBinary(String a, String b) { 3 if(a == null||b == null) 4 return ""; 5 if(a.length() == 0) 6 return b; 7 if(b.length() == 0) 8 return a; 9 Stack stack = new Stack();10 ...
阅读全文
摘要:use stack 1 public class Solution { 2 public int[] plusOne(int[] digits) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(digits == null) 6 return null; 7 Stack myst...
阅读全文
摘要:recursion programming 1 public class Solution { 2 public int uniquePaths(int m, int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int[][] result = new int[m+1][n+1]; 6 result...
阅读全文
摘要:1 public class Solution { 2 public int lengthOfLastWord(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(s == null) 6 return 0; 7 char[] mychar = s.trim()...
阅读全文
摘要:recursion programming 1 public class Solution { 2 public String countAndSay(int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(n == 1) 6 return "1"; 7 char[] my...
阅读全文
摘要:use three 2-d matrix to store if any column, row or block has more than one specific value. 1 public class Solution { 2 public boolean isValidSudoku(char[][] board) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for ...
阅读全文
摘要:simple recursion programming problemelse if(target < A[mid]) return binarySearch(A, start, mid, target); else return binarySearch(A, mid+1, end, target); 1 public class Solution { 2 public int searchInsert(int[] A, int target) { 3 // IMPORTANT: Please res...
阅读全文
摘要:same asRemove Duplicates from Sorted Array 1 public class Solution { 2 public int removeElement(int[] A, int elem) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(A == null||A.length <...
阅读全文
摘要:use two index to compress array 1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(A == null||A.length < 1) 6 ...
阅读全文
摘要:recursion programming 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public class Solution {13 public ListNode swapPairs(List...
阅读全文
摘要:fastrunner and slowrunner trick.(be care of head node) 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public class Solution {13 ...
阅读全文
摘要:recursion problem 1 public class Solution { 2 public ArrayList letterCombinations(String digits) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 ...
阅读全文
摘要:similar with 3Sum 1 public class Solution { 2 public int threeSumClosest(int[] num, int target) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(num == null || num.length < 3) 6 ...
阅读全文
摘要:1 public class Solution { 2 public ArrayList generateParenthesis(int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 StringBuilder builde...
阅读全文
摘要:1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public class Solution {13 public ListNode mergeTwoLists(ListNode l1, ListNode...
阅读全文
摘要:use stack (be careful of empty stack) 1 public class Solution { 2 public boolean isValid(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(s == null||s.length() == 0) 6 ...
阅读全文
摘要:be careful of the special cases(null, 0) 1 public class Solution { 2 public String longestCommonPrefix(String[] strs) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(strs == null||strs...
阅读全文
摘要:当前一位小于后一位的时候才是后一位减去前一位,并且这种位数只有一位 1 public class Solution { 2 public int romanToInt(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int result = 0; 6 if (s == null || s....
阅读全文
摘要:use stringbuffer 1 public class Solution { 2 public String intToRoman(int num) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 StringBuffer result = new StringBuffer(); 6 String[]...
阅读全文
摘要:brute force O(n2)从头尾两头扫面 O(n) 1 public class Solution { 2 public int maxArea(int[] height) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int start = 0; 6 int end = height.length...
阅读全文
摘要:注意负数-2147483648的绝对值还是自己!!!(overflow) 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if( x 0)11 {12 mask...
阅读全文
摘要:1.trim2.符号3.overflow 1 public class Solution { 2 public int atoi(String str) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(str == null) 6 return 0; 7 if(str.lengt...
阅读全文
摘要:use PriorityQueue( Priority Heap)the time complexity will be O(k * lgn) 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public cla...
阅读全文
摘要:用stack,并记录入栈的括号的位置 1 public class Solution { 2 class Node{ 3 char c; 4 int nub; 5 public Node(char c, int nub){ 6 this.c = c; 7 this.nub = nub; 8 } 9 }10 11 public int longestValidParentheses(String s) {12 // IMPORTANT: ...
阅读全文
摘要:两次二分法查找 1 public class Solution { 2 public boolean searchMatrix(int[][] matrix, int target) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int line = searchLine(matrix,0,matrix.length,ta...
阅读全文
摘要:直接乘的话会超时O(n)。利用二分法降为O(lgn)。 1 public class Solution { 2 public double pow(double x, int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(n == 0) 6 return 1; 7 if(...
阅读全文
摘要:If result is overflow, then return -1. 1 public class Solution { 2 public int reverse(int x) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 boolean positive = true; 6 if(x 0)11 ...
阅读全文
摘要:寻找W型字符的规律:除第一和最后一行外都是对称的。找出循环的长度相应的做出位移即可。比较烦 1 public class Solution { 2 public String convert(String s, int nRows) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(nRows == 1) 6 ...
阅读全文
摘要:从左往右扫描,当遇到重复字母时,以上一个重复字母的index +1,作为新的搜索起始位置。可以减少时间, 但时间复杂度没有变O(n2) 1 public class Solution { 2 public int lengthOfLongestSubstring(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ...
阅读全文
摘要:use dynamic programming to exchange time with space. 1 public class Solution { 2 public int uniquePaths(int m, int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int[][] mytable = new...
阅读全文
摘要:very easy recursion1 public class Solution {2 public int maxDepth(TreeNode root) {3 // IMPORTANT: Please reset any member data you declared, as4 // the same Solution instance will be reused for each test case.5 if(root == null)6 return 0;7 return Math....
阅读全文
摘要:特殊值可先判断并返回 很重要本题是通过左移代替乘法产生不同的除数, 然后做减法得到解。 1 public class Solution { 2 public int divide(int dividend, int divisor) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(dividend == 0) 6 ...
阅读全文
摘要:当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:以i为根节点的树,其左子树由[0, i-1]构成, 其右子树由[i+1, n]构成。再由递归便可得:public static int numTrees(int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int[] myarray = new int[n+...
阅读全文
|