- 博客(51)
- 收藏
- 关注
原创 关于单调栈的使用
表现良好的最长时间段--这道题的关键就是前缀和,以及单调栈。前缀和意思是说如果大于8就是1否则就是-1,这样可以形成新的数组,然后新的数组中如果两个数中后面的数大于前面则这个区间就是有效区间。求有效区间的最大值,使用单调栈,先遍历这个数组获取递减值,然后反向遍历数组,和单调栈pop()出来的元素进行比对,并要注意i的相对大小。这用到的关键思想就是递减区间中一定包含最小值。每日温度也用到了单调栈...
2020-02-05 15:13:42
242
原创 回溯法的思考
最近在二刷leetcode之前做过的题。对回溯法有新的认识。之前在做类似于全排列的题,for循环中每一个新的 状态我都会new LinkedList<>(oldlist),然后之后的深度搜索会使用这个list。但,这个解题方法应该停止,每一个新的状态new一个list会限制增加时间复杂度因为newLinkedList<>(oldlist)会遍历整个数组!正确的解...
2020-01-29 23:05:09
273
原创 java的自定义排序
java的自定义排序,(o1,o2)->{ code... return 1;} PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> { String frontstr = o1+...
2020-01-28 22:41:38
315
原创 leetcode-二叉树的中序遍历
class Solution { private List<Integer> retlist = new LinkedList<>(); public List<Integer> inorderTraversal(TreeNode root) { //中序遍历,左臂入栈 TreeNode cur = root;...
2020-01-26 21:50:31
222
原创 leetcode-两两交换链表中的节点
class Solution { public ListNode swapPairs(ListNode head) { return swap(head); } public ListNode swap(ListNode leftnode){ if(leftnode==null || leftnode.next == null){ ...
2020-01-26 20:06:03
119
原创 二分查找java模板
public static int binarySearch(int[] array,int first,int last,int value){ while(first<last){ int mid = first+(last-first)/2; if(array[mid]<value){ ...
2020-01-07 17:12:37
212
原创 leetcode-两数之和
class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer,Integer> map = new HashMap<>(); for(int i=0;i<nums.length;i+=1){ int num ...
2019-12-31 20:17:36
116
原创 leetcode-根据身高重建队列
class Solution { public int[][] reconstructQueue(int[][] people) { if(people.length==0){ return people; } Arrays.sort(people, (o1, o2) -> (o1[0]==o2[0])?o1[...
2019-12-31 20:16:20
206
原创 leetcode-游戏玩法分析
# Write your MySQL query statement belowselect player_id,min(event_date) as first_login from activity a group by player_id ;这道题我明白了,group by 之后可以使用各种聚合函数例如min,max求值!...
2019-12-31 20:15:52
508
原创 leetcode-螺旋矩阵
class Solution { private List<Integer> retlist = new LinkedList<>(); private int rowlength; private int columnlength; public List<Integer> spiralOrder(int[][] matri...
2019-12-31 20:14:37
158
原创 leetcode-岛屿的最大面积
class Solution { private int[][] dp; private int maxcount = 0; public int maxAreaOfIsland(int[][] grid) { if(grid.length==0){ return 0; } this.dp = new...
2019-12-31 20:04:33
207
原创 leetcode-排序链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode sortList(L...
2019-12-31 20:01:40
122
原创 leetcode-文章浏览
# Write your MySQL query statement belowselect distinct author_id as id from Views where author_id = viewer_id order by author_id很简单的一道题....
2019-12-31 19:51:15
190
原创 leetcode-缺失的第一个正整数
class Solution { public int firstMissingPositive(int[] nums) { Arrays.sort(nums); int likenum = 1; for(int i=0;i<nums.length;i+=1){ int num = nums[i]; ...
2019-12-31 19:49:09
222
原创 leetcode-矩阵中的最长递增路径
class Solution { private HashMap<String,Integer> maxmap = new HashMap<>(); private int[][] matrix; private int maxcount = 0; private int rowlength; private int columnl...
2019-12-31 19:40:26
258
原创 leetcode-每位学生的最高成绩
# Write your MySQL query statement belowselect e.student_id,(select course_id from Enrollments en where en.student_id=e.student_id and en.grade = max(e.grade) order by course_id limit 1) as course_i...
2019-12-31 19:34:04
268
原创 leetcode-比特位计数
class Solution { public int[] countBits(int num) { int[] ret = new int[num+1]; ret[0] = 0; for(int i=1;i<=num;i+=1){ ret[i] = ret[i&(i-1)]+1; }...
2019-12-29 15:31:31
149
原创 leetcode-二叉树的最近公共祖先
class Solution { private TreeNode ans; public Solution() { // Variable to store LCA node. this.ans = null; } private boolean recurseTree(TreeNode currentNode, TreeN...
2019-12-29 15:05:53
127
原创 leetcode-买卖股票的最佳时机
class Solution { public int maxProfit(int[] prices) { if(prices.length==0){ return 0; } if(prices.length==1){ return 0; } int nowmo...
2019-12-29 15:02:25
107
原创 leetcode-Average Selling Price
# Write your MySQL query statement belowselect p.product_id,round(sum(p.price*u.units)/sum(u.units),2) as average_price from Prices p join UnitsSold u on p.product_id = u.product_id where u.purchas...
2019-12-29 14:55:38
511
原创 leetcode-二叉树的右视图
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...
2019-12-29 14:47:54
291
原创 leetcode-分数排名
# Write your MySQL query statement belowselect Score , (select count(distinct Score) from Scores where Score>=s.Score) as Rank from Scores s order by Score desc从这道题可以看出,可以广泛的思考利用子查询求解局部。还有就是子...
2019-12-29 14:44:44
744
原创 leetcode-重排链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public void reorderList(Li...
2019-12-29 14:40:15
219
1
原创 leetcode-将每个元素替换为右侧最大元素
class Solution { public int[] replaceElements(int[] arr) { LinkedList<Integer> list = new LinkedList<>(); if(arr.length==1){ return new int[]{-1}; ...
2019-12-29 14:34:13
148
原创 leetcode-转变数组后最接近目标值的数组和
class Solution { public int findBestValue(int[] arr, int target) { int length = arr.length; Arrays.sort(arr); int minindex = -1; int mincount = -1; for(int...
2019-12-29 14:32:01
294
原创 leetcode-层数最深叶子节点的和
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pri...
2019-12-29 14:28:58
333
原创 leetcode-子串的最大出现次数
class Solution { public int maxFreq(String s, int maxLetters, int minSize, int maxSize) { HashMap<String,Integer> map = new HashMap<>(); for(int i=0;i+minSize<=s.le...
2019-12-27 10:55:53
341
原创 leetcode-划分数组为连续数字的集合
class Solution { public boolean isPossibleDivide(int[] nums, int k) { int len = nums.length; if (len % k != 0) { return false; } PriorityQueue<Int...
2019-12-26 21:53:52
329
原创 leetcode-最后一块石头的重量
class Solution { public int lastStoneWeight(int[] stones) { PriorityQueue<Integer> queue = new PriorityQueue<>((o1,o2)->o2-o1); for(int i=0;i<stones.length;i+=1){ ...
2019-12-26 21:35:15
123
原创 leetcode-至少合作三次的导演和演员
# Write your MySQL query statement belowselect ACTOR_ID,DIRECTOR_ID from ActorDirector group by actor_id,director_id having count(director_id)>=3可以看出sql允许通过多个字段进行group by。筛选null值可以通过 is not ...
2019-12-26 21:31:49
139
原创 leetcode-乘积最大子序列
class Solution { public int maxProduct(int[] nums) { if(nums.length==0){ return 0; } if(nums.length==1){ return nums[0]; } i...
2019-12-26 21:22:45
154
原创 leetcode-单词拆分
class Solution { public boolean wordBreak(String s, List<String> wordDict) { if(wordDict.size()==0){ return false; } int[] dp = new int[s.length()]; ...
2019-12-26 20:54:19
117
原创 leetcode-旋转图像
class Solution { public void rotate(int[][] matrix) { if(matrix==null){ return; } if(matrix.length==1){ return ; } //先转置,再镜像对称 ...
2019-12-26 20:47:28
129
原创 leetcode-分发糖果
class Solution { public int candy(int[] ratings) { if(ratings==null){ return 0; } if(ratings.length==1){ return 1; } int count =0;...
2019-12-26 20:42:11
195
1
原创 leetcode-合并两个有序链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode mergeTwoLi...
2019-12-26 20:34:56
166
原创 leetcode-买卖股票的最佳时机 II
class Solution { public int maxProfit(int[] prices) { if(prices==null){ return 0; } if(prices.length==0){ return 0; } if(prices.len...
2019-12-26 20:30:26
91
原创 leetcode-买卖股票的最佳时机 |||
class Solution { public int maxProfit(int[] prices) { if(prices.length==0){ return 0; } int[][][] dp = new int[prices.length][2][3]; dp[0][0][1] = -pri...
2019-12-26 20:26:23
104
原创 leetcode-组合两个表
# Write your MySQL query statement belowselect p.FirstName,p.LastName,a.City,a.State from Person p left join Address a on p.PersonId=a.PersonId 因为题目中要求如果address表中没有对应数据也需要显示。所以使用left join,意思是左表中每一...
2019-12-25 21:56:28
109
原创 leetcode-换座位
# Write your MySQL query statement beloselect (case when mod(id,2) = 1 and id != (select count(*) from seat) then id+1 when mod(id,2) = 0 then id-1 when mod(id,2) = 1 and id=(select coun...
2019-12-25 21:52:51
216
原创 leetcode-第十行
# Read from the file file.txt and output the tenth line to stdout.awk 'NR==10{print}' file.txt这道题需要我们编写bash脚本输出文本文件中的第十行我们使用awk这一编程语言NR意思是number of record。根据分隔符判断number。默认的分隔符是换行符在awk中单引号' ...
2019-12-25 21:40:29
160
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人