- 博客(70)
- 问答 (1)
- 收藏
- 关注
原创 [leetcode]75. Sort Colors
Solution 1: 2-pass+counting sort+3个extra space类似与桶排序,先pass一遍,记下0,1,2的个数然后重新对数组赋值class Solution { public void sortColors(int[] nums) { int count1=0,count2=0,count0=0; for(int i=0;i...
2018-12-28 19:23:37 96
原创 [leetcode]74. Search a 2D Matrix
Solution 1: binary search整个二维数组是已经排好序了的。第一反应是降维,把二维的变成一维数组,然后应用二分搜索法class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix.length==0||matrix[0].length==0)r...
2018-12-28 14:02:13 87
原创 [leetcode]73. Set Matrix Zeroes
Solution 1: O(MN)class Solution { public void setZeroes(int[][] matrix) { int[][] newMatrix=new int[matrix.length][matrix[0].length]; for(int i=0;i<matrix.length;i++){ ...
2018-12-27 20:59:56 75
原创 [leetcode]71. Simplify Path
Solution 1:genius,我的想法是老老实实一个一个char压栈,结果应该先分割!!!class Solution { public String simplifyPath(String path) { Stack<String> stack = new Stack(); for(String cur: path.split("/")){ ...
2018-12-24 20:16:24 70
原创 [leetcode]69. Sqrt(x)
Solution 1: 二分搜索自己想出来的推导关系:根号x &lt;= 1+x/2 (x为从0到Integer.MAX_VALUE)证明: 两边同时平方得: x &lt;= x2+x+10 &lt;=x2+1 一定成立,所以得证如果正好开尽,在循环中就return了如果不能正好开尽,那么我们要找最靠右的那个(类似于上天花板数)循环退出条件是low+1==hi, 因为low找的是平方...
2018-12-24 16:02:21 124
原创 [leetcode]68. Text Justification
这一题的思路想对了,就是middle justify的时候,当一行空格不均匀分配的处理没想清楚。不知道这题考的点在哪里class Solution { public List<String> fullJustify(String[] words, int maxWidth) { //res保存每一个string里的单词个数 List<I...
2018-12-23 21:36:32 83
原创 [leetcode]67. Add Binary
Solution 1class Solution { public String addBinary(String a, String b) { int n=Math.max(a.length(),b.length()); int[] nums=new int[n+1]; //先把能对齐的部分加起来 int...
2018-12-23 13:12:22 62
原创 [leetcode]66. Plus One
MY DUMMY SOLUTIONclass Solution { public int[] plusOne(int[] digits) { int c=0; digits[digits.length-1]=digits[digits.length-1]+1; if(digits[digits.length-1]>=10){ ...
2018-12-23 11:41:16 157
原创 [leetcode]65. Valid Number
We start with trimming.If we see [0-9] we reset the number flags.We can only see . if we didn't see e or ..We can only see e if we didn't see e but we did see a number. We reset numberAfterE flag....
2018-12-23 10:56:06 71
原创 [leetcode]61. Rotate List
忘记哪一题了,反正和前面有一题很像,挺简单的Solution 1: 利用循环链表O(n)int dis=count-k%count;利用这个式子,将问题转换为–找新的head node即可/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode ...
2018-12-23 10:30:29 75
原创 [leetcode]60. Permutation Sequence
temptation1: 按排列数的方法写,排序方式不一样class Solution { public String getPermutation(int n, int k) { int[] nums=new int[n+1]; List<String> reslist=new ArrayList<String>(); ...
2018-12-22 15:24:43 66
原创 [leetcode]59. Spiral Matrix II
这题和54题一摸一样,基本就easy把class Solution { public int[][] generateMatrix(int n) { int r1=0; int r2=n-1; int c1=0; int c2=n-1; int[][] matrix=new...
2018-12-22 12:32:41 57
原创 [leetcode]58. Length of Last Word
从后往前,先找到一个非空格的,开始计数,一直向前找,如果找到一个空格停下。利用两个空格之间,是一个单词的原理class Solution { public int lengthOfLastWord(String s) { int tail=s.length()-1; int len=0; while(tail>=0&&...
2018-12-22 12:12:14 67
原创 [leetcode]57. Insert Interval
/** * 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; } * } */...
2018-12-22 11:37:04 79
原创 [leetcode]56. Merge Intervals
solution 1: 第一反应是divide&amp;conquer(最初用了sort+nlogn的时间复杂度)但是发现不能按照老套路来,因为有的不需要合并,所以这里的merge处理方法比较特别list&lt;&gt;的自定义sorthttp://www.cnblogs.com/printN/p/6974960.html/** * Definition for an interval....
2018-12-21 17:06:22 96
原创 [bootstrap]一个简单的bootstrap4的card的demo
&lt;!doctype html&gt;&lt;html lang="en"&gt;&lt;head&gt; &lt;!-- Required meta tags --&gt; &lt;meta charset="utf-8"&gt; &
2018-12-20 21:09:57 3661 1
原创 几种二分搜索总结
1.如果有重复,找的是最左边(leftmost)的那个的index。如果不存在,返回的是应该插入的index,这里数组长度是可变的。见一个easy题 35题class Solution { public int searchInsert(int[] nums, int target) { int lo=0; int hi=n...
2018-12-20 14:03:20 138
原创 [leetcode]300. Longest Increasing Subsequence
Solution 1: dpclass Solution { public int lengthOfLIS(int[] nums) { if(nums.length==0)return 0; int[] dp=new int[nums.length+1]; dp[0]=1; int max...
2018-12-20 13:24:57 94
原创 [leetcode]78. Subsets
Solution 1:backtrack妈呀,被虐了几天了,终于直接ac了一题,太tm激动了。class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res=new ArrayList<>();...
2018-12-19 21:11:19 126
原创 416. Partition Equal Subset Sum
Solution 1:backtrack(TLE)我怎么这么喜欢回溯,该dp的题目一回溯就超时class Solution { public boolean canPartition(int[] nums) { int total=0; for(int i=0;i&lt;nums.length;i++){ total+=nums...
2018-12-19 19:44:15 49
原创 377. Combination Sum IV
Solution 1: backtrack(TLE)class Solution { public int combinationSum4(int[] nums, int target) { Arrays.sort(nums); int ans=0; return backtrack(nums, target, 0, ans); }...
2018-12-19 15:37:28 96
原创 [leetcode]216. Combination Sum III
这个问题就是一个回溯的子集树的变形,还比较简单。Solution 1:backtrack1去重:只有递增的list才是加入res这个去重的方法导致时间复杂度很高class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<...
2018-12-19 11:23:41 52
原创 [leetcode]140. Word Break II
class Solution { public List<String> wordBreak(String s, List<String> wordDict) { Set<Integer> set = new HashSet<>(); StringBuffer res=new StringBuffer(); ...
2018-12-19 10:10:13 106
原创 [leetcode]股票买卖问题总结
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/Most-consistent-ways-of-dealing-with-the-series-of-stock-problems反反复复看了好几遍,终于算是看懂了。总结一下。Therefore o...
2018-12-18 12:50:26 1186
原创 bootstrap 4总结
navbar-dark bg-light实际改变只是text-color,脑回路是这样子的:既然navbar主题是dark,那么相应的text一定是light的。所以如果navbar-light bg-light的话,就全看不见了。...
2018-12-17 19:00:17 404
原创 [leetcode]338. Counting Bits
solution 1:因为乘以2相当于左移,最右添0,所以1的个数都不变。 (这一句的思想是精髓)奇数i的个数相当于是前一个偶数,末尾+1得到的。而前一个偶数中1的个数=这个偶数/2的1的个数。又因为偶数和其后一个奇数除以二,都得到同一个偶数。(比如4和2的个数一样,5/2也是2)。所以奇数偶数的式子可以合并f[i] = f[i >> 1] + (i & 1);如果i是...
2018-12-17 15:32:44 57
原创 [leetcode]304. Range Sum Query 2D - Immutable
整体的思路一样。就是dp这个怎么求没想出来class NumMatrix { private int[][] dp; public NumMatrix(int[][] matrix) { if (matrix.length == 0 || matrix[0].length == 0) return; dp = new int[matri...
2018-12-17 12:39:07 71
原创 [leetcode]303. Range Sum Query - Immutable
solution 1: 暴力class NumArray { private int[] nums; private int sum; public NumArray(int[] nums) { this.nums=nums; } public int sumRange(int i, int j) { this.s...
2018-12-17 12:35:27 68 1
原创 [leetcode]403. Frog Jump
知道是dp,但是没有思路Use map to represent a mapping from the stone (not index) to the steps that can be taken from this stone.so this will be[0,1,3,5,6,8,12,17]{17=[], 0=[1], 1=[1, 2], 3=[1, 2, 3], 5=[1...
2018-12-17 10:12:31 130
原创 [leetcode]96. Unique Binary Search Trees
https://www.cnblogs.com/zyt1253679098/p/9190217.htmlsolution 1: calalan数的递归式注意要换成long,否则会出错class Solution { public int numTrees(int n) { long[] dp=new long[n+1]; dp[1]=1l; ...
2018-12-16 15:42:20 66
原创 [leetcode]139. Word Break
solution 1: 自己写的递归,tle了class Solution { public boolean wordBreak(String s, List&amp;amp;amp;amp;amp;lt;String&amp;amp;amp;amp;amp;gt; wordDict) { if(s.length()==0||s==null)return true; String spre=&amp;
2018-12-16 13:32:34 69
原创 [leetcode]62. Unique Paths
非常简单没什么好说的class Solution { public int uniquePaths(int m, int n) { int[][] dp=new int[m+1][n+1]; for(int i=0;i<=m;i++){ for(int j=0;j<=n;j++){ ...
2018-12-16 11:52:57 89
原创 [udemy]bootstrap+html+css
boilerplate<!DOCTYPE html><html><head> <title>perfect match </title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/boo
2018-12-14 22:08:06 147
原创 [leetcode]264. Ugly Number II
Solution 1: 参照数学质因子的定义,大概规模能跑到800,900,就会超时我这个方法也太暴力了。https://blog.csdn.net/fulishafulisha/article/details/79984101class Solution { public int nthUglyNumber(int n) { int count=0; ...
2018-12-14 15:38:00 87
原创 [leetcode]746. Min Cost Climbing Stairs
Solution 1:递归 tleclass Solution { public int minCostClimbingStairs(int[] cost) { int n=cost.length-1; int start=-1; return help(cost,start,n); } public...
2018-12-14 14:34:36 72
原创 [leetcode]Most consistent ways of dealing with the series of stock problems
慢慢理解这个思想https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/Most-consistent-ways-of-dealing-with-the-series-of-stock-problems
2018-12-14 12:53:54 120
原创 [leetcode]189. Rotate Array
Solution1: 第一反应就是暴力,竟然也过了class Solution { public void rotate(int[] nums, int k) { for(int m=0;m<k;m++){ int temp=nums[nums.length-1]; for(int i=nums.length-1;i&...
2018-12-13 15:00:49 134
原创 [leetcode]198. House Robber
class Solution { public int rob(int[] nums) { if(nums.length==0)return 0; int[] memo=new int[nums.length+1]; memo[0]=0; memo[1]=nums[0]; for(int i=2;i<...
2018-12-13 14:20:01 78
原创 [leetcode]213. House Robber II
这里的数组的i值得是 到第i家为止的钱0的话表示一家都还没有。public class Solution { public int rob(int[] nums) { if (nums.length == 0) return 0; if (nums.length < 2) return nums[0]...
2018-12-13 14:16:08 95
原创 [leetcode]152. Maximum Product Subarray
53题的升级版https://mp.csdn.net/mdeditor/84856498#class Solution { public int maxProduct(int[] nums) { int[] premax=new int[nums.length]; int[] premin=new int[nums.length]; pr...
2018-12-13 12:19:24 81
空空如也
用hibernate连mysql出错
2017-12-03
TA创建的收藏夹 TA关注的收藏夹
TA关注的人