09 2018 档案

摘要:1 //用list比较快 2 class Solution { 3 public String frequencySort(String s) { 4 if(s.length() == 0 || s.length() == 1) return s; 5 HashMap map = new HashMap(); 6 cha... 阅读全文
posted @ 2018-09-28 06:08 jasoncool1
摘要:1 class Solution { 2 public int islandPerimeter(int[][] grid) { 3 if(grid == null) return 0; 4 int row = grid.length; 5 if(row == 0) return 0; 6 int col = gr... 阅读全文
posted @ 2018-09-28 02:34 jasoncool1
摘要:https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/82450/Java-O(m+n)-using-Hash-and-List 阅读全文
posted @ 2018-09-27 12:17 jasoncool1
摘要:str.trim.split(" +") 然后用StringBuilderhttps://leetcode.com/problems/reverse-words-in-a-string/discuss/161008/6-line-Java-answer 阅读全文
posted @ 2018-09-27 10:37 jasoncool1
摘要:可以用priorityqueuePriorityQueue<Integer> pq = new PriorityQueue<>(); https://leetcode.com/problems/third-maximum-number/discuss/90190/Java-PriorityQueue 阅读全文
posted @ 2018-09-27 10:06 jasoncool1
摘要:1 class Solution { 2 public String longestCommonPrefix(String[] strs) { 3 if(strs.length == 0) return ""; 4 StringBuilder sb = new StringBuilder(""); 5 for(int i = 0;... 阅读全文
posted @ 2018-09-26 10:29 jasoncool1
摘要:1 class Solution { 2 public boolean isPalindrome(int x) { 3 if(x < 0) return false; 4 String str = "" + x; 5 StringBuilder sb = new StringBuilder(str); 6 return sb... 阅读全文
posted @ 2018-09-26 09:39 jasoncool1
摘要:1 class Solution { 2 public int reverse(int x) { 3 long x2 = (long)x; 4 if(x == 0) return 0; 5 if(x2 Integer.MAX_VALUE) { 22 return 0; 23 ... 阅读全文
posted @ 2018-09-26 04:21 jasoncool1
摘要:1 class Solution { 2 List> res = new ArrayList(); 3 public List> partition(String s) { 4 if(s.length() == 0) return res; 5 dfs(s, 0, new ArrayList() ); 6 return ... 阅读全文
posted @ 2018-09-23 23:58 jasoncool1
摘要:不知道说啥好,从边缘是O的开始标记不可能变的点,这样时间复杂度只有O(mn) 阅读全文
posted @ 2018-09-23 23:29 jasoncool1
摘要:1 //New 2 class Solution { 3 public int sumNumbers(TreeNode root) { 4 if(root == null) return 0; 5 return dfs(root, 0); 6 7 } 8 9 public int dfs(Tre... 阅读全文
posted @ 2018-09-23 11:29 jasoncool1
摘要:Set会快很多替换各个位置的字符 在wordDict里面判断存不存在类似BFShttps://blog.csdn.net/u014532901/article/details/78820124 阅读全文
posted @ 2018-09-23 10:58 jasoncool1
摘要:1 class Solution { 2 public int maxProfit(int[] prices) { 3 int len = prices.length; 4 if(len == 0 || len == 1) return 0; 5 int res = 0; 6 int buy = prices[0... 阅读全文
posted @ 2018-09-23 00:09 jasoncool1
摘要:从下面往上 可以建立一个cache 这个点往下search过已经知道最小值了 下一次访问的时候就可以直接取值, 因为一般一个点都会被上面访问两次,如果不这样就会time limit错误 阅读全文
posted @ 2018-09-22 23:37 jasoncool1
摘要:1 class Solution { 2 List res = new ArrayList(); 3 public List getRow(int rowIndex) { 4 helper(1, rowIndex + 1, new ArrayList(), null); 5 return res; 6 7 }... 阅读全文
posted @ 2018-09-22 04:29 jasoncool1
摘要:1 class Solution { 2 List> res = new ArrayList(); 3 public List> generate(int numRows) { 4 helper(1, numRows, new ArrayList(), null); 5 return res; 6 7 }... 阅读全文
posted @ 2018-09-22 00:25 jasoncool1
摘要:1 class Solution { 2 public void connect(TreeLinkNode root) { 3 if(root == null) return; 4 Queue queue = new LinkedList(); 5 queue.offer(root); 6 int size = ... 阅读全文
posted @ 2018-09-21 23:49 jasoncool1
摘要:1 class Solution { 2 public void connect(TreeLinkNode root) { 3 if(root == null) return; 4 Queue queue = new LinkedList(); 5 queue.offer(root); 6 int size = ... 阅读全文
posted @ 2018-09-21 23:44 jasoncool1
摘要:把左边=null, 把最左边遍历到null 保证已经flatten 然后再弄右边 然后把root.right跟左边连接,再把右边连接到root.right的最下面 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/dis 阅读全文
posted @ 2018-09-21 23:03 jasoncool1
摘要:1 class Solution { 2 List> res = new ArrayList(); 3 public List> pathSum(TreeNode root, int sum) { 4 if(root == null) return res; 5 backtrack(root, sum, new ArrayList());... 阅读全文
posted @ 2018-09-21 09:11 jasoncool1
摘要:只有在两个子节点都是null的叶节点才有可能符合条件\ 阅读全文
posted @ 2018-09-21 08:46 jasoncool1
摘要:注意只有一个子节点的情况,分开讨论 阅读全文
posted @ 2018-09-21 08:02 jasoncool1
摘要:1 class Solution { 2 public boolean isBalanced(TreeNode root) { 3 if(root == null) return true; 4 return(isBalanced(root.left) && isBalanced(root.right) && Math.abs(dfs(root.l... 阅读全文
posted @ 2018-09-21 07:49 jasoncool1
摘要:https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/35476/Share-my-JAVA-solution-1ms-very-short-and-concise. 阅读全文
posted @ 2018-09-20 10:29 jasoncool1
摘要:1 class Solution { 2 public TreeNode sortedArrayToBST(int[] nums) { 3 int size = nums.length; 4 return helper(nums, 0, size - 1); 5 6 } 7 8 public Tr... 阅读全文
posted @ 2018-09-20 07:32 jasoncool1
摘要:1 class Solution { 2 public List> levelOrderBottom(TreeNode root) { 3 Queue queue = new LinkedList(); 4 List> res = new ArrayList(); 5 if(root == null) return res; 6... 阅读全文
posted @ 2018-09-20 06:56 jasoncool1
摘要:1 class Solution { 2 public TreeNode buildTree(int[] inorder, int[] postorder) { 3 return helper(postorder.length-1, 0, inorder.length - 1, inorder, postorder); 4 5 } 6... 阅读全文
posted @ 2018-09-20 06:44 jasoncool1
摘要:1 //New 用一个记录queue.size()就可以 2 class Solution { 3 public List> zigzagLevelOrder(TreeNode root) { 4 List> res = new ArrayList(); 5 if(root == null) return res; 6 Queu... 阅读全文
posted @ 2018-09-20 05:54 jasoncool1
摘要:https://leetcode.com/problems/symmetric-tree/discuss/166375/Java-Accepted-very-simple-solution 阅读全文
posted @ 2018-09-20 04:28 jasoncool1
摘要:分成两边, left right排列组合加到root, lo==hi就返回当前值 阅读全文
posted @ 2018-09-18 10:57 jasoncool1
摘要:https://leetcode.com/problems/unique-binary-search-trees/discuss/31666/DP-Solution-in-6-lines-with-explanation.-F(i-n)-G(i-1)-*-G(n-i) 阅读全文
posted @ 2018-09-17 12:47 jasoncool1
摘要:1 //Recursive 2 class Solution { 3 List res = new ArrayList(); 4 public List inorderTraversal(TreeNode root) { 5 if (root != null) { 6 inorderTraversal(root.left)... 阅读全文
posted @ 2018-09-17 05:49 jasoncool1
摘要:https://leetcode.com/problems/restore-ip-addresses/discuss/30944/Very-simple-DFS-solution 阅读全文
posted @ 2018-09-17 04:01 jasoncool1
摘要:1 class Solution { 2 public ListNode reverseBetween(ListNode head, int m, int n) { 3 ListNode node1 = head; 4 // int len = 0; //可以不用加 5 // while(node1 != nu... 阅读全文
posted @ 2018-09-17 01:01 jasoncool1
摘要:https://leetcode.com/problems/gray-code/discuss/29893/One-liner-Python-solution-(with-demo-in-comments) 阅读全文
posted @ 2018-09-16 23:55 jasoncool1
摘要:1 class Solution { 2 public void merge(int[] nums1, int m, int[] nums2, int n) { 3 int[] res = new int[m + n]; 4 int pos = 0; 5 int i = 0, j = 0; 6 while(i =... 阅读全文
posted @ 2018-09-16 11:07 jasoncool1
摘要:1 class Solution { 2 public ListNode partition(ListNode head, int x) { 3 ListNode newHead = null; 4 ListNode node2 = null; 5 ListNode node1 = head; 6 whi... 阅读全文
posted @ 2018-09-16 10:38 jasoncool1
摘要:1 class Solution { 2 public ListNode deleteDuplicates(ListNode head) { 3 if(head == null || head.next == null) return head; 4 ListNode node1 = head; 5 ListNode node2 ... 阅读全文
posted @ 2018-09-16 10:06 jasoncool1
摘要:pre.next设置成下一个数字的第一个 下一个数字进行循环 循环到相同的数字的最后一个 要是pre.next = node1 说明只有unique pre=pre.next, 否则说明不unique pre.next设置成下一个数字的第一个 就是pre.next = node1.next http 阅读全文
posted @ 2018-09-16 03:13 jasoncool1
摘要:1 class Solution { 2 public boolean search(int[] nums, int target) { 3 if(nums.length == 0) return false; 4 int lo = 0, hi = nums.length-1; 5 while(lo target && targ... 阅读全文
posted @ 2018-09-16 00:08 jasoncool1
摘要:1 class Solution { 2 public int removeDuplicates(int[] nums) { 3 if(nums.length == 0) return 0; 4 if(nums.length == 1) return 1; 5 int i = 1, j = 1; 6 int co... 阅读全文
posted @ 2018-09-15 22:59 jasoncool1
摘要:1 class Solution { 2 List> res = new ArrayList(); 3 public List> combine(int n, int k) { 4 if(k == 0) return null; 5 int[] nums = new int[n]; 6 for(int i = 0; i ... 阅读全文
posted @ 2018-09-15 22:27 jasoncool1
摘要:1 class Solution { 2 public void sortColors(int[] nums) { 3 if(nums == null || nums.length == 0) return; 4 int count0 = 0; 5 int count1 = 0; 6 int count2 = 0... 阅读全文
posted @ 2018-09-15 12:23 jasoncool1
摘要:用Binary search 先搜索row 再搜索col 阅读全文
posted @ 2018-09-15 11:52 jasoncool1
摘要:可以用String.join 方法 String.join("/", stack);每个元素之间用"/"分开 阅读全文
posted @ 2018-09-14 11:53 jasoncool1
摘要:1 class Solution { 2 public String addBinary(String a, String b) { 3 if(a.length() == 0 && b.length() == 0) return null; 4 StringBuilder sb1 = new StringBuilder(a); 5 ... 阅读全文
posted @ 2018-09-14 10:42 jasoncool1
摘要:1 class Solution { 2 public int minPathSum(int[][] grid) { 3 if(grid == null) return 0; 4 int row = grid.length; 5 int col = grid[0].length; 6 int[][] res = ... 阅读全文
posted @ 2018-09-14 09:49 jasoncool1
摘要:1 class Solution { 2 public int uniquePathsWithObstacles(int[][] obstacleGrid) { 3 if(obstacleGrid == null) return 0; 4 int row = obstacleGrid.length; 5 int col = obs... 阅读全文
posted @ 2018-09-14 09:12 jasoncool1
摘要:1 class Solution { 2 public ListNode rotateRight(ListNode head, int k) { 3 if(head == null) return null; 4 int n = 0; 5 ListNode node1 = head; 6 ListNode las... 阅读全文
posted @ 2018-09-13 13:21 jasoncool1
摘要:1 class Solution { 2 public int[] plusOne(int[] digits) { 3 int n = digits.length; 4 int[] res = new int[n+1]; 5 int carry = 0; 6 for(int i = n - 1; i >= 0; ... 阅读全文
posted @ 2018-09-13 11:52 jasoncool1
摘要:https://leetcode.com/problems/permutation-sequence/discuss/22508/An-iterative-solution-for-reference 阅读全文
posted @ 2018-09-13 11:08 jasoncool1
摘要:跟spiral matrix类似 阅读全文
posted @ 2018-09-13 07:23 jasoncool1
摘要:要是都是空格的话 返回的array长度就是0 阅读全文
posted @ 2018-09-12 02:20 jasoncool1
摘要:backtrack 阅读全文
posted @ 2018-09-11 06:41 jasoncool1
摘要:backtrack 阅读全文
posted @ 2018-09-11 06:39 jasoncool1
摘要:https://leetcode.com/problems/multiply-strings/discuss/17605/Easiest-JAVA-Solution-with-Graph-Explanation 阅读全文
posted @ 2018-09-11 00:16 jasoncool1
摘要:避免这个循环的重复 所以i>position && nums[i] == nums[i-1] 阅读全文
posted @ 2018-09-10 21:03 jasoncool1
摘要:为了避免前面选了 到后面选的时候 又选到了后面的前面 加一个position作为标记 循环从position开始 阅读全文
posted @ 2018-09-10 11:47 jasoncool1
摘要:1 class Solution { 2 public String countAndSay(int n) { 3 String str = "1"; 4 if(n == 1) return str; 5 return helper(str, n - 1); 6 7 8 } 9 ... 阅读全文
posted @ 2018-09-10 11:14 jasoncool1
摘要:https://leetcode.com/problems/valid-sudoku/discuss/15472/Short+Simple-Java-using-Strings 阅读全文
posted @ 2018-09-10 09:34 jasoncool1
摘要:对mid两边进行binary search 得出边界 阅读全文
posted @ 2018-09-10 06:14 jasoncool1
摘要:easy最后返回的是nums.length 阅读全文
posted @ 2018-09-08 08:05 jasoncool1
摘要:https://leetcode.com/problems/next-permutation/discuss/13865/Sharing-my-clean-and-easy-understand-java-code-with-explanation?page=3 在当前序列中,从尾端往前寻找两个相邻 阅读全文
posted @ 2018-09-08 07:51 jasoncool1
摘要:1 class Solution { 2 public int divide(int dividend, int divisor) { 3 long count = 0; 4 if(dividend == 0) return 0; 5 //divisor和dividend都要变成正数和long再进行处理 6 if... 阅读全文
posted @ 2018-09-08 05:34 jasoncool1
摘要:String相等 == 只是比较引用值就是地址如果是new String的话 例如substring跟其他的比较就要用str.equal() 阅读全文
posted @ 2018-09-07 23:06 jasoncool1
摘要:easy 阅读全文
posted @ 2018-09-07 22:44 jasoncool1
摘要:reverse都是两个记录要交换的 一个记录之前的 corner:null 有一个 有两个 阅读全文
posted @ 2018-09-07 21:53 jasoncool1
摘要:别想复杂 就一个记录标的 一个记录要对比的 阅读全文
posted @ 2018-09-06 22:07 jasoncool1
摘要:backtrack 再熟悉一下 阅读全文
posted @ 2018-09-06 21:32 jasoncool1
摘要:恢复内容开始 不一定都要用StringBuilder 可能用String更方便https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/8118/Easy-understand-Java-Solution 阅读全文
posted @ 2018-09-06 11:32 jasoncool1
摘要:注意标准设定的大小不能超过Integer.MAX_VALUE 阅读全文
posted @ 2018-09-06 10:15 jasoncool1
摘要:substring要是取不到东西不会返回"" 而是什么都不返回,所以不能用== "" 判断 要用长度来判断。 阅读全文
posted @ 2018-09-06 09:11 jasoncool1
摘要:list的反转Collections.sort(list, Collections.reverseOrder()); 阅读全文
posted @ 2018-09-04 23:33 jasoncool1
摘要:StringBuffer类可以append reverse insert deleteCharAt setCharAt 其实直接用String的array也ok 阅读全文
posted @ 2018-09-04 10:05 jasoncool1
摘要:直接用一个carry记录进位就可以 阅读全文
posted @ 2018-09-04 09:11 jasoncool1
摘要:https://leetcode.com/problems/top-k-frequent-elements/discuss/126241/java 先记录次数 然后按次数放进数组 然后从后往前取出 阅读全文
posted @ 2018-09-03 06:58 jasoncool1
摘要:身体不好 阅读全文
posted @ 2018-09-02 11:19 jasoncool1
摘要:注意转换类型 别忘了加long 阅读全文
posted @ 2018-09-02 06:54 jasoncool1
摘要:身体不好 阅读全文
posted @ 2018-09-02 04:42 jasoncool1