- 博客(103)
- 收藏
- 关注
原创 蓄水池算法382. Linked List Random Node
碰到一个用到了蓄水池算法的题目:http://blog.jobbole.com/42550//** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x;
2017-07-05 02:41:44
392
原创 [leetCode刷题笔记]373. Find K Pairs with Smallest Sums
public class Solution { public List kSmallestPairs(int[] nums1, int[] nums2, int k) { PriorityQueue queue = new PriorityQueuea[0]+a[1]-b[0]-b[1]); List res = new ArrayList<>();
2017-06-27 12:38:43
407
原创 [leetCode刷题笔记]113. Path Sum II
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {
2017-06-25 23:46:02
366
原创 [leetCode刷题笔记]47. Permutations II
思路是用一个Boolean array来保存每个元素用过的记录,如果用过,则跳过。处理重复数的时候,先sort,然后如果前一个元素等于后一个元素而且前一个元素已经用过,则跳过后一个元素public class Solution { public List> permuteUnique(int[] nums) { List res = new ArrayLi
2017-06-23 08:23:44
396
原创 [leetCode刷题笔记]260. Single Number III
public class Solution { public int[] singleNumber(int[] nums) { int xor = 0; for (int num : nums) { xor ^= num; } xor &= -xor;
2017-06-22 10:13:15
348
原创 [leetCode刷题笔记]385. Mini Parser
/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * public interface NestedInteger { * // Constructo
2017-06-20 10:17:05
348
原创 [leetCode刷题笔记]112. Path Sum
要是到eft才算。。。。中间的不算的。。。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } *
2017-06-19 01:34:01
282
原创 [leetCode刷题笔记]347. Top K Frequent Elements
public class Solution { public List topKFrequent(int[] nums, int k) { // frequency array List[] freList = new List[nums.length + 1]; // key: value in nums[] value: frequenc
2017-06-17 12:17:57
312
原创 [leetCode刷题笔记]598. Range Addition II
无聊的奇葩题目。。。public class Solution { public int maxCount(int m, int n, int[][] ops) { if (ops == null || ops.length == 0) return m * n; int col = Integer.MAX_VALUE;
2017-06-17 11:33:29
330
原创 [leetCode刷题笔记]599. Minimum Index Sum of Two Lists
public class Solution { public String[] findRestaurant(String[] list1, String[] list2) { int i = 0; i = 0; Map map2 = new HashMap(); for (String ele : list2 ) {
2017-06-12 02:12:23
469
原创 [leetCode刷题笔记]371. Sum of Two Integers
对bit运算实在不了解"&" AND operation, for example, 2 (0010) & 7 (0111) => 2 (0010)"^" XOR operation, for example, 2 (0010) ^ 7 (0111) => 5 (0101)"~" NOT operation, for example, ~2(0010) => -3 (1101)
2017-06-11 23:53:32
266
原创 [leetCode刷题笔记]520. Detect Capital
public class Solution { public boolean detectCapitalUse(String word) { int count = 0; for(char c: word.toCharArray()) if('Z' - c >= 0) count++; return (
2017-06-11 22:15:45
318
原创 [leetCode刷题笔记]606. Construct String from Binary Tree
奇葩的规则,左子树的规则必须打。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public cl
2017-06-09 10:13:25
842
原创 [leetCode刷题笔记]575. Distribute Candies
public class Solution { public int distributeCandies(int[] candies) { Arrays.sort(candies); int len = candies.length; int cur = candies[0]; int di
2017-06-09 09:47:12
382
原创 [leetCode刷题笔记]500. Keyboard Row
public class Solution { public String[] findWords(String[] words) { List sl = new ArrayList(); Map map = new HashMap<>(); String[] key = {"QWERTYUIOP", "ASDFGHJKL", "ZXCV
2017-06-08 10:03:50
331
原创 [leetCode刷题笔记]566. Reshape the Matrix
public class Solution { public int[][] matrixReshape(int[][] nums, int r, int c) { if (nums == null || nums.length == 0 || r * c != nums.length * nums[0].length) return nums; int[]
2017-06-06 11:24:40
304
原创 [leetCode刷题笔记]561. Array Partition I
public class Solution { public int arrayPairSum(int[] nums) { Arrays.sort(nums); int result = 0; for (int i = 0; i < nums.length; i+=2) { result += nums[i];
2017-06-05 08:09:57
402
原创 [leetCode刷题笔记]199. Binary Tree Right Side View
这道题不难,但是要掌握技巧。用recursion来。 递归时记下树的深度,当深度等于list的大小时候加入到list里面,并且优先遍历右子树。/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode righ
2017-06-05 07:33:50
339
原创 [leetCode刷题笔记]450. Delete Node in a BST
如果碰到要删除node有两个子node,那么找出右子树的最小值,在赋给node,然后删去最小值原来的那个node/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNod
2017-06-04 10:11:55
337
原创 [leetCode刷题笔记]409. Longest Palindrome
第一种解法: HashSet.将String中的char一个个放入Set中,先检查有没有,如果有的话删除,没有的话加入。public class Solution { public int longestPalindrome(String s) { if (s == null || s.length() == 0) { return 0;
2017-06-04 07:33:46
562
原创 Spring MVC中的session解决方案
之前用PHP做小组项目中用到了session value。在PHP中session value的处理很简单而直接。现在用Spring MVC写项目发现网上的关于Spring mvc 的Session教程写的都不太清楚。所以参考stackoverflow:https://stackoverflow.com/questions/18791645/how-to-use-session-attribu
2017-05-23 01:49:50
600
原创 [leetCode刷题笔记]49. Group Anagrams
将String进行排序,再做成key插入map中,map的value是listpublic class Solution { public List> groupAnagrams(String[] strs) { if (strs == null || strs.length == 0) return new ArrayList>(); Map> str
2017-05-19 06:58:33
289
转载 [SSM]改用SpringMVC
之前Struts2弄了半天,结果别人说Struts2过时了。。。。我是个紧跟时代潮流的人,那就改用SpringMVC吧,先上个网址:http://www.cnblogs.com/bigdataZJ/p/springmvc1.html
2017-05-17 03:36:58
422
原创 [leetCode刷题笔记]300. Longest Increasing Subsequence
DP解决,用dp[len]表示遍历到的最大值,新的值如果大于这个值len++否则替换dp中的一个len不变public class Solution { public int lengthOfLIS(int[] nums) { //use dp[len] to store the largest val. int[] dp = new int[nu
2017-04-28 08:00:19
321
原创 [leetCode刷题笔记]129. Sum Root to Leaf Numbers
public class Solution { public int sumNumbers(TreeNode root) { return helper(root, 0); } private int helper (TreeNode root, int s) { if (root == null) return 0; if (
2017-04-27 11:38:28
268
原创 [leetCode刷题笔记]525. Contiguous Array
用类似动态规划的思路来解决。用HashMap是因为HashMap查找key更快。public class Solution { public int findMaxLength(int[] nums) { // change 0 to -1 for (int i = 0; i < nums.length; i++) { if
2017-04-27 10:34:47
537
原创 [leetCode刷题笔记]284. Peeking Iterator
// Java Iterator interface reference:// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.htmlclass PeekingIterator implements Iterator { private Iterator ite; private Integer val
2017-04-26 09:43:07
283
原创 [leetCode刷题笔记]494. Target Sum
用递归做,但是比较慢。网上有用动态规划做的http://blog.youkuaiyun.com/hit0803107/article/details/54894227public class Solution { int res = 0; public int findTargetSumWays(int[] nums, int S) { if (nums == nu
2017-04-26 04:59:17
694
原创 [leetCode刷题笔记]22. Generate Parentheses
还是backtrack 一开始一对一对的加括号结果出错了,因为一对括号只能包第一个。还是两个括号分开加。用left和right表示左右括号的数量。public class Solution { public List generateParenthesis(int n) { List res= new ArrayList(); helper(res,
2017-04-25 07:56:56
447
原创 [leetCode刷题笔记]173. Binary Search Tree Iterator
用stack来存储node,先存储最左边的node,再往右移动public class BSTIterator { Stack stack = new Stack(); public BSTIterator(TreeNode root) { pushLeft(root); } /** @return whether we have a nex
2017-04-24 22:36:46
321
原创 [leetCode刷题笔记]46. Permutations
还是用backtrack呀public class Solution { public List> permute(int[] nums) { List> res = new ArrayList>(); helper(res, new ArrayList(), nums); return res; } private vo
2017-04-24 08:37:00
324
原创 [leetCode刷题笔记]341. Flatten Nested List Iterator
思路是这样的:做一个stack,然后在constructor方法中将nestedList中所有元素都添加到这个stack中(最后的元素在最底下)。hasNext方法中,如果stack空了,返回false,否则,先看栈顶元素是否为int,若不为int则getlist再将所有元素压入栈中。如果是int则退出循环public class NestedIterator implements Itera
2017-04-24 05:30:12
508
原创 [leetCode刷题笔记]516. Longest Palindromic Subsequence
动态规划解决问题,dp[][]存放string(i,j)之间palim长度。public class Solution { public int longestPalindromeSubseq(String s) { int[][] dp = new int[s.length()][s.length()]; for (int i = s
2017-04-23 02:35:03
299
原创 [leetCode刷题笔记]491. Increasing Subsequences
这道题用hashset存放list,这样就不会出现重复的情况。如果array里面任何数都不相同,那么不用Set应该也可以public class Solution { public List> findSubsequences(int[] nums) { // using hash set to remove duplicate element Set>
2017-04-22 23:09:17
304
原创 [leetCode刷题笔记]77. Combinations
使用backtrack的方法。对过程进行递归。public class Solution { public List> combine(int n, int k) { List> combs = new ArrayList>(); combine(combs, new ArrayList(), 1, n, k); return combs
2017-04-22 02:57:31
347
原创 [leetCode刷题笔记]504. Base 7
public class Solution { public String convertToBase7(int num) { if (num < 0) { return "-" + convertToBase7(-num); } if (num < 7) { return num + "";
2017-04-21 23:13:13
421
原创 [leetCode刷题笔记]100. Same Tree
递归很简单public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if (p != null && q != null) { if (p.val != q.val) return false; else {
2017-04-21 22:39:11
372
原创 [leetCode刷题笔记]503. Next Greater Element II
用一个stack来存储之前的index要遍历两遍array因为存在循环。public class Solution { public int[] nextGreaterElements(int[] nums) { int n = nums.length; int[] res = new int[n]; Arrays.fill(res,
2017-04-21 11:10:34
450
原创 [leetCode刷题笔记]475. Heaters
这道题读题读了半天,大致意思是。给两个数组,第一个数组表示房间的位置,第二个表示加热器的位置,然后想通过设置加热器覆盖的半径来让所有屋子都被加热器覆盖。第一种方法就是找到每个屋子毗邻的加热器,然后再得到所需要最小半径。public class Solution { public int findRadius(int[] houses, int[] heaters) {
2017-04-21 00:03:21
1358
原创 [leetCode刷题笔记]515. Find Largest Value in Each Tree Row
515. Find Largest Value in Each Tree Row使用递归,通过一个数d记录所处树的深度。还有通过比较d和list的长度来确定是添加一个新值,还是比较旧值与新值的大小。注意List的引用一直不变public class Solution { public List largestValues(TreeNode root) { Lis
2017-04-20 09:02:04
532
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅