提交 34e359cf 编写于 作者: L liu13

20190703

上级 87dc8f81
package code;
/*
* 1025. Divisor Game
* 1025. Maximum Difference Between Node and Ancestor
* 题意:父节点减子节点的绝对值最大
* 难度:
* 分类:
* 思路:自己写的自下向上,返回的时候再计算。
* 可以自顶向下的,到叶子节点计算就可以
* 可以自顶向下的,把路径上的最大值和最小值传到叶子节点,到叶子节点再计算
* Tips:
*/
public class lc1026 {
......@@ -44,4 +44,15 @@ public class lc1026 {
res[1] = Math.min(Math.min(left[1], right[1]), root.val);
return res;
}
public int maxAncestorDiff2(TreeNode root) {
return dfs(root, root.val, root.val);
}
public int dfs(TreeNode root, int mn, int mx) {
if (root == null) return mx - mn;
mx = Math.max(mx, root.val);
mn = Math.min(mn, root.val);
return Math.max(dfs(root.left, mn, mx), dfs(root.right, mn, mx));
}
}
......@@ -4,7 +4,7 @@ package code;
* 题意:
* 难度:Medium
* 分类:
* 思路:lc56 类似的题,但该题的集合没有传递覆盖的特性,所以遍历的时候又加了个循环,N^2
* 思路: lc56 类似的题,但该题的集合没有传递覆盖的特性,所以遍历的时候又加了个循环,N^2
* 非常巧妙的方法,每次记录上车,下车人数,然后遍历一遍进行模拟上下车
* Tips:lc253, lc56
*/
......
......@@ -6,7 +6,7 @@ package code;
* 分类:Tree, Depth-first Search
* 思路:因为二叉树只有两个节点,一条路径可以想象成倒V字,从低层的某个节点一路向上,到达一个顶点,再一路向下,理解了这一点,整道题就好解了。
* Tips:用了一个全局变量存储最后结果,因为函数返回的是直线路径上的最优解,而不是V字路径最优解
* lc112, lc113, lc437, lc129, lc124, lc337, lc543
* lc112, lc113, lc437, lc129, lc124, lc337, lc543, lc1026
*/
public class lc124 {
public class TreeNode {
......
......@@ -6,6 +6,7 @@ package code;
* 分类:Design
* 思路:hashmap + 双向链表。hashmap实现了O(1)的get,双向链表实现O(1)的put
* Tips:能想到双向链表,就不难了
* lc380
*/
import java.util.HashMap;
......
......@@ -10,7 +10,7 @@ import java.util.LinkedList;
* 难度:Hard
* 分类:Tree, Design
* 思路:
* Tips:
* Tips:lc572 序列化的应用
*/
public class lc297 {
public class TreeNode {
......
......@@ -5,7 +5,7 @@ import java.util.PriorityQueue;
/*
* 378. Kth Smallest Element in a Sorted Matrix
* 题意:在矩阵中搜索第k的数,横轴和纵轴都是有序的
* 题意:在矩阵中搜索第k的数,横轴和纵轴都是有序的
* 难度:Medium
* 分类:Binary Search, Heap
* 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。
......
......@@ -10,7 +10,7 @@ import java.util.List;
* 难度:Medium
* 分类:Array, Hash Table, Design
* 思路:List 的插入和删除都是O(1), 通过hashmap绑定来使得Get也为O(1)
* Tips:
* Tips:和LRU哪个题类似 lc146
*/
public class lc380 {
public class RandomizedSet {
......
......@@ -13,21 +13,21 @@ public class lc395 {
public int longestSubstring(String s, int k) {
int res = 0;
for (int i = 1; i <= 26 ; i++) {
int left = 0, right = 0, cur_uni_char = 0, less_than_k_char = 0;
int left = 0, right = 0, cur_uni_char = 0, more_than_k_char = 0;
int[] map = new int[26];
while(right<s.length()){ //右边推进
map[s.charAt(right)-'a']++;
if(map[s.charAt(right)-'a']==k) less_than_k_char++;
if(map[s.charAt(right)-'a']==k) more_than_k_char++;
if(map[s.charAt(right)-'a']==1) cur_uni_char++;
right++;
if( cur_uni_char==i && less_than_k_char==i) res = Math.max(res, right-left);
if( cur_uni_char==i && more_than_k_char==i) res = Math.max(res, right-left);
else if(cur_uni_char>i){ //左边推进。不在外边加上一个循环的话,就不知道怎么推荐左指针了。
while(cur_uni_char!=i){
map[s.charAt(left)-'a']--;
if(map[s.charAt(left)-'a']==0) cur_uni_char--;
if((map[s.charAt(left)-'a']==k-1)) less_than_k_char--;
if((map[s.charAt(left)-'a']==k-1)) more_than_k_char--;
left++;
}
}
......
......@@ -7,6 +7,7 @@ import java.util.HashSet;
* 难度:Medium
* 分类:Dynamic Programming
* 思路:题意可以转换为用任意个元素组成的和等于数组和/2。可以和 lc1, lc15 3-Sum 对比。
* dfs过不了,2^n
* 0,1背包问题,递推比较简单,所以空间可以压缩成一维
* 自己想的思路其实和压缩后的0,1背包类似,但没想到该问题可以抽象为0,1背包
* dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]]
......@@ -67,4 +68,6 @@ public class lc416 {
}
return dp[volumn];
}
}
......@@ -13,6 +13,7 @@ import java.util.HashMap;
* 和lc560有共同的思想,每个节点只需遍历一遍就可以了
* 虽然是Easy题,做好也不简单
* lc112, lc113, lc437, lc129, lc124, lc337
* lc437 lc572 一样的递归思路
* lc303, lc437, lc560
*/
public class lc437 {
......
......@@ -6,6 +6,8 @@ package code;
* 分类:Tree
* 思路:两种方法,一种是先序遍历,然后比较字符串即可,注意每个节点开始前加个字符,null也要加进去。
* 另一种递归的方法。
* lc437 lc572 一样的递归思路
* lc297 序列化
* Tips:
*/
public class lc572 {
......
......@@ -7,7 +7,7 @@ import java.util.Stack;
* 难度:Medium
* 分类:Stack, Greedy
* 思路:匹配的都出栈,最后剩下的栈中没匹配的个数,就是需要添加的个数
* Tips:
* Tips:lc301
*/
public class lc921 {
public int minAddToMakeValid(String S) {
......
......@@ -8,7 +8,7 @@ package code;
* Tips:lc53
*/
public class lc978 {
public int maxTurbulenceSize(int[] A) {
public int maxTurbulenceSize(int[] A) { //太难想了
int inc = 1, dec = 1, result = 1;
for (int i = 1; i < A.length; i++) {
if (A[i] < A[i - 1]) { // +1 并且重置另一个统计量
......@@ -25,4 +25,27 @@ public class lc978 {
}
return result;
}
public int maxTurbulenceSize2(int[] A) {
int[] arr = new int[A.length-1];
for(int i=1; i<A.length; i++){
if(A[i]==A[i-1]) arr[i-1] = 0;
else if(A[i]>A[i-1]) arr[i-1] = 1;
else arr[i-1] = -1;
}
int res = 1;
boolean flag = false; //判断下是否全是0, 返回的时候就不+1
int count = 1;
for(int i=1; i<arr.length; i++){
if(arr[i]!=0) flag=true;
if(arr[i]==-arr[i-1] && arr[i]!=0) {
count++;
}else{
res = Math.max(res, count);
count = 1;
}
}
res = Math.max(res, count);
return flag? res+1: res;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册