- 博客(76)
- 收藏
- 关注
原创 leetcode题解(七十三):309. Best Time to Buy and Sell Stock with Cooldown
问买卖股票最多能收益多少,这道题和之前的不同是买卖一次之后需要经历一个冷却期public int maxProfit(int[] prices) { int sell = 0, prev_sell = 0, buy = Integer.MIN_VALUE, prev_buy; for (int price : prices) { prev_buy = buy;...
2019-06-03 01:57:14
165
原创 leetcode题解(七十二):300. Longest Increasing Subsequence
題意:給出一個數組,求最長遞增子序列的長度Example:Input: [10,9,2,5,3,7,101,18]Output: 4Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.public int lengthOfLIS(int[] nums) { /...
2019-06-02 00:53:37
172
原创 leetcode题解(七十一):287. Find the Duplicate Number
题意:找出一个数组中唯一的重复元素Example 1:Input: [1,3,4,2,2]Output: 2Example 2:Input: [3,1,3,4,2]Output: 3比较巧妙的思路:把数组想象成链表来做,跟链表是否有环的一题解题思路很像int findDuplicate3(vector<int>& nums){ if (nums.size()...
2019-05-17 02:43:32
154
原创 leetcode题解(七十):283. Move Zeroes
public void moveZeroes(int[] nums) { if (nums == null || nums.length == 0) return; int insertPos = 0; for (int num: nums) { if (num != 0) nums[insertPos++] = num; } ...
2019-05-06 16:18:54
132
原创 leetcode题解(六十九):279. Perfect Squares
输入一个数,输出是他是几个数的平方和用动态规划来做public int numSquares(int n) { int[] dp = new int[n + 1]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for(int i = 1; i <= n; ++i) { int min = Integer.MAX_VALUE;...
2019-05-06 16:09:38
150
原创 leetcode题解(六十八):240. Search a 2D Matrix II
这道题public class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix == null || matrix.length < 1 || matrix[0].length <1) { return false; ...
2019-05-06 15:57:38
196
原创 leetcode题解(六十七):239. Sliding Window Maximum
public int[] maxSlidingWindow(int[] a, int k) { if (a == null || k <= 0) { return new int[0]; } int n = a.length; int[] r = new int[n-k+1]; int ri = 0; // store index Deque<I...
2019-05-06 14:44:36
161
原创 leetcode题解(六十六):238. Product of Array Except Self
求一个数组中每一个元素除了它本身的积Example:Input: [1,2,3,4]Output: [24,12,8,6]思路:左右两边同时遍历就可以节省时间public class Solution {public int[] productExceptSelf(int[] nums) { int n = nums.length; int[] res = new i...
2019-05-05 14:48:42
129
原创 leetcode题解(六十五):236. Lowest Common Ancestor of a Binary Tree
二叉树两个节点的最低公共祖先public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null || root == p || root == q) return root; TreeNo...
2019-05-05 14:41:44
139
原创 leetcode题解(六十四):234. Palindrome Linked List
判断是不是回文序列,中心对称Example 1:Input: 1->2Output: falseExample 2:Input: 1->2->2->1Output: true用快慢指针,链表反转来做public boolean isPalindrome(ListNode head) { ListNode fast = head, slow = hea...
2019-05-05 14:34:29
123
原创 leetcode题解(六十三):226. Invert Binary Tree
转换左右子树,还是树的递归public TreeNode invertTree(TreeNode root) { if (root == null) { return null; } //递归下去 TreeNode right = invertTree(root.right); TreeNode left = invertTree(roo...
2019-05-05 14:29:25
146
转载 leetcode题解(六十二):221. Maximal Square
二维数组中找1构成的面积最大的矩形Example:Input:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Output: 4public int maximalSquare(char[][] a) { if(a.length == 0) return 0; int m = a.length, n = a[0].length, result...
2019-05-05 02:10:29
135
原创 leetcode题解(六十一):215. Kth Largest Element in an Array
找数组中第k大的元素可以用快排的partition函数来做public int findKthLargest(int[] nums, int k) { //打乱初始顺序是关键 shuffle(nums); k = nums.length - k; int lo = 0; int hi = nums.length - 1; ...
2019-05-05 02:06:43
147
原创 leetcode题解(六十):208. Implement Trie (Prefix Tree)
例子:Example:Trie trie = new Trie();trie.insert(“apple”);trie.search(“apple”); // returns truetrie.search(“app”); // returns falsetrie.startsWith(“app”); // returns truetrie.insert(“app”);...
2019-05-05 01:50:55
123
原创 leetcode题解(五十九):207. Course Schedule
课程安排,输入是课程总数,上课的方式,输出是否可行Example 1:Input: 2, [[1,0]]Output: trueExplanation: There are a total of 2 courses to take.To take course 1 you should have finished course 0. So it is possible.Example ...
2019-05-04 14:38:48
127
原创 leetcode题解(五十八):206. Reverse Linked List
反转链表,基本操作Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLpublic ListNode reverseList(ListNode head) { /* iterative solution */ ListNode newHead =...
2019-05-03 13:13:01
146
原创 leetcode题解(五十七):200. Number of Islands
二维数组中数数岛的个数岛指由1构成的四周都是0(上下左右)的地方Example 1:Input:11110110101100000000Output: 1Example 2:Input:11000110000010000011Output: 3public class Solution {private int n;private int m;public...
2019-05-03 13:01:49
138
原创 leetcode题解(五十六):198. House Robber
抢劫,不能抢相邻两家店铺问最多能抢多少典型的动态规划问题Example 1:Input: [1,2,3,1]Output: 4Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).Total amount you can rob = 1 + 3 = 4.Example 2:Input: [2,7...
2019-05-03 12:50:26
118
原创 leetcode题解(五十五):169. Majority Element
这个算法流计算的课上面讲过, 就是计算一个数组中出现次数最多的元素用一个counter做加减法就可以Example 1:Input: [3,2,3]Output: 3Example 2:Input: [2,2,1,1,1,2,2]Output: 2public class Solution { public int majorityElement(int[] num) {...
2019-05-03 12:36:09
140
原创 leetcode题解(五十四):160. Intersection of Two Linked Lists
找两个链表的连接点public ListNode getIntersectionNode(ListNode headA, ListNode headB) { //boundary check //边界条件 if(headA == null || headB == null) return null; ListNode a = headA; Lis...
2019-05-02 13:32:27
111
原创 leetcode题解(五十三):155. Min Stack
最小堆可以用两个栈来实现,也可以用一个栈实现例子:MinStack minStack = new MinStack();minStack.push(-2);minStack.push(0);minStack.push(-3);minStack.getMin(); --> Returns -3minStack.pop();minStack.top(); --&g...
2019-05-02 13:25:23
114
原创 leetcode题解(五十二):152. Maximum Product Subarray
题意:求最大的子数组的积一般这种题都是动态规划做的因为是乘积,所以不仅要记下最大,还要记下最小Example 1:Input: [2,3,-2,4]Output: 6Explanation: [2,3] has the largest product 6.Example 2:Input: [-2,0,-1]Output: 0Explanation: The result can...
2019-05-02 13:07:35
133
原创 leetcode题解(五十一):148. Sort List
单链表排序用的是归并排序的思路递归下去就好Example 1:Input: 4->2->1->3Output: 1->2->3->4Example 2:Input: -1->5->3->4->0Output: -1->0->3->4->5public class Solution { p...
2019-05-01 14:11:53
279
原创 leetcode题解(五十):142. Linked List Cycle II
如果链表有环,找到成环的节点public class Solution { public ListNode detectCycle(ListNode head) { ListNode slow = head; ListNode fast = head; whi...
2019-04-27 12:02:58
118
原创 leetcode题解(四十九):141 Linked List Cycle
判断链表是否有环思路:快慢指针public boolean hasCycle(ListNode head) { if(head==null) return false; ListNode walker = head; ListNode runner = head; while(runner.next!=null && runner.next.ne...
2019-04-27 12:00:37
101
原创 leetcode题解(四十八):139. Word Break
给一个字符串和一个字典,判断字符串是否能被拆分成字典里的单词Example 1:Input: s = “leetcode”, wordDict = [“leet”, “code”]Output: trueExplanation: Return true because “leetcode” can be segmented as “leet code”.Example 2:Input:...
2019-04-27 11:43:29
103
原创 leetcode题解(四十七):136. Single Number
找出一个数组中只出现了一次的元素思路就是XORExample 1:Input: [2,2,1]Output: 1Example 2:Input: [4,1,2,1,2]Output: 4int singleNumber(int A[], int n) { int result = 0; for (int i = 0; i<n; i++) { res...
2019-04-26 20:44:35
77
原创 leetcode题解(四十六):128. Longest Consecutive Sequence
求最长连续序列的长度Input: [100, 4, 200, 1, 3, 2]Output: 4Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.public int longestConsecutive(int[] num) { int...
2019-04-26 20:37:08
134
原创 leetcode题解(四十五)124. Binary Tree Maximum Path Sum
求二叉树最大的路径和,可以不过树根public class Solution { int maxValue; public int maxPathSum(TreeNode root) { maxValue = Integer.MIN_VALUE; maxPathDown(root); return maxValue; ...
2019-04-26 20:31:35
88
原创 leetcode题解(四十四):121. Best Time to Buy and Sell Stock
买卖股票,动态规划问题Example 1:Input: [7,1,5,3,6,4]Output: 5Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.Not 7-1 = 6, as selling price needs to be larger than buyin...
2019-04-26 20:22:27
92
原创 leetcode题解(四十三):114. Flatten Binary Tree to Linked List
把二叉树变成链表看图片应该是前序遍历private TreeNode prev = null;public void flatten(TreeNode root) { if (root == null) return; flatten(root.right); flatten(root.left); root.right = prev; ...
2019-04-25 15:59:35
357
原创 leetcode题解(四十二):105. Construct Binary Tree from Preorder and Inorder Traversal
给出前序和中序遍历,构造二叉树通过前序遍历得到根节点,中序遍历得到左右子树,递归下去就可以了public TreeNode buildTree(int[] preorder, int[] inorder) { return helper(0, 0, inorder.length - 1, preorder, inorder);}public TreeNode helper(in...
2019-04-25 15:47:22
246
原创 leetcode题解(四十一):104. Maximum Depth of Binary Tree
求二叉树的深度简单题基本操作,递归思路public int maxDepth(TreeNode root) { if(root==null){ return 0; } return 1+Math.max(maxDepth(root.left),maxDepth(root.right)); }...
2019-04-25 15:19:47
139
原创 leetcode题解(四十):102. Binary Tree Level Order Traversal
二叉树的层序遍历按照输入输出先进先出的思路,这里用栈结构public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> queue = new LinkedList<TreeNode>(); ...
2019-04-25 15:10:25
136
原创 leetcode题解(三十九):101. Symmetric Tree
判断一棵二叉树是不是对称的思路:简单递归public boolean isSymmetric(TreeNode root) { return root==null || isSymmetricHelp(root.left, root.right);}private boolean isSymmetricHelp(TreeNode left, TreeNode right){ ...
2019-04-25 00:27:35
108
原创 leetcode题解(三十八):96. Unique Binary Search Trees
Input: 3Output: 5Explanation:Given n = 3, there are a total of 5 unique BST’s:public int numTrees(int n) { // G[n]表示n有几个可能 int [] G = new int[n+1]; G[0] = G[1] = 1; for(int i=2;...
2019-04-24 23:36:23
114
原创 leetcode题解(三十七):94. Binary Tree Inorder Traversal
中序遍历二叉树Input: [1,null,2,3]12/3Output: [1,3,2]顺序:左中右思路就是简单迭代,使用栈结构public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<Integer>();...
2019-04-24 23:17:18
155
原创 leetcode题解(三十六):85. Maximal Rectangle
给一个二维数组,找出由1组成的最大矩形Input:[[“1”,“0”,“1”,“0”,“0”],[“1”,“0”,“1”,“1”,“1”],[“1”,“1”,“1”,“1”,“1”],[“1”,“0”,“0”,“1”,“0”]]Output: 6class Solution {public:int maximalRectangle(vector<vector<cha...
2019-04-24 23:00:17
150
原创 sql删除表中重复字段(去重)
最近在做比赛,有重复的请求记录要删除,百度了一下解法,比如我有一行数据:a,b,c在表中重复出现,去重的做法如下:DELETE FROM 表 WHERE id NOT IN ( SELECT temp.min_id FROM ( SELECT MIN(id) min_id FROM 表 GROUP BY a,b,c )AS temp );...
2019-04-23 22:49:38
332
原创 leetcode题解(三十五):84. Largest Rectangle in Histogram
给一个数组代表直方图,求最大的矩形面积given height = [2,1,5,6,2,3].area = 10 unit.Example:Input: [2,1,5,6,2,3]Output: 10public static int largestRectangleArea(int[] height) { if (height == null || height.len...
2019-04-23 22:46:23
158
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅