提交 eb04b27e 编写于 作者: L liu13

20190415

上级 22819ff6
package code;
/*
* 1025. Divisor Game
* 题意:
* 难度:
* 分类:
* 思路:先选的有主动权
* Tips:
*/
public class lc1025 {
public boolean divisorGame(int N) {
if(N%2==0) return true;
return false;
}
}
package code;
/*
* 1025. Divisor Game
* 题意:父节点减子节点的绝对值最大
* 难度:
* 分类:
* 思路:自己写的自下向上,返回的时候再计算。
* 可以自顶向下的,到叶子节点计算就可以
* Tips:
*/
public class lc1026 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
int res_val = 0;
public int maxAncestorDiff(TreeNode root) {
helper(root);
return res_val;
}
public int[] helper(TreeNode root){
int[] res = new int[2]; //存一个最大,一个最小
if(root.left==null&&root.right==null){
res[0] = root.val;
res[1] = root.val;
return res;
}
int[] left = new int[2];
int[] right = new int[2];
left[0] = Integer.MIN_VALUE;
right[0] = Integer.MIN_VALUE;
left[1] = Integer.MAX_VALUE;
right[1] = Integer.MAX_VALUE;
if(root.left!=null) left = helper(root.left); //可能为空,只有一边有节点
if(root.right!=null) right = helper(root.right);
res_val = Math.max(Math.abs(root.val - Math.max(left[0], right[0])), res_val);
res_val = Math.max(Math.abs(root.val - Math.min(left[1], right[1])), res_val);
res[0] = Math.max(Math.max(left[0], right[0]), root.val); //别忘了和root节点本身的值比
res[1] = Math.min(Math.min(left[1], right[1]), root.val);
return res;
}
}
package code;
/*
* 1027. Longest Arithmetic Sequence
* 题意:最长等差数列
* 难度:Medium
* 分类:Dynamic Programming
* 思路:还是要在每个位置上都记录一下,不是说一个大的数组就可以了。因为相同长度的等差,后续数字往上接的时候不一定接哪一个,都要保存,并不是说一定去接小的那个。
* 二维dp, 一维记录位置,一维记录差分
* Tips:很棒的题
*/
public class lc1027 {
public static int longestArithSeqLength(int[] A) {
int res = 0;
int[][] dp = new int[A.length][20000];
for (int i = 0; i < A.length ; i++) {
for (int j = 0; j < i ; j++) {
int ind = A[i]-A[j]+10000;
dp[i][ind] = dp[j][ind] + 1;
res = Math.max(res, dp[i][ind]);
}
}
return res+1; //加1
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册