File tree Expand file tree Collapse file tree 3 files changed +86
-0
lines changed Expand file tree Collapse file tree 3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change
1
+ package code ;
2
+ /*
3
+ * 1025. Divisor Game
4
+ * 题意:
5
+ * 难度:
6
+ * 分类:
7
+ * 思路:先选的有主动权
8
+ * Tips:
9
+ */
10
+ public class lc1025 {
11
+ public boolean divisorGame (int N ) {
12
+ if (N %2 ==0 ) return true ;
13
+ return false ;
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ package code ;
2
+ /*
3
+ * 1025. Divisor Game
4
+ * 题意:父节点减子节点的绝对值最大
5
+ * 难度:
6
+ * 分类:
7
+ * 思路:自己写的自下向上,返回的时候再计算。
8
+ * 可以自顶向下的,到叶子节点计算就可以
9
+ * Tips:
10
+ */
11
+ public class lc1026 {
12
+ public class TreeNode {
13
+ int val ;
14
+ TreeNode left ;
15
+ TreeNode right ;
16
+ TreeNode (int x ) {
17
+ val = x ;
18
+ }
19
+ }
20
+ int res_val = 0 ;
21
+ public int maxAncestorDiff (TreeNode root ) {
22
+ helper (root );
23
+ return res_val ;
24
+ }
25
+
26
+ public int [] helper (TreeNode root ){
27
+ int [] res = new int [2 ]; //存一个最大,一个最小
28
+ if (root .left ==null &&root .right ==null ){
29
+ res [0 ] = root .val ;
30
+ res [1 ] = root .val ;
31
+ return res ;
32
+ }
33
+ int [] left = new int [2 ];
34
+ int [] right = new int [2 ];
35
+ left [0 ] = Integer .MIN_VALUE ;
36
+ right [0 ] = Integer .MIN_VALUE ;
37
+ left [1 ] = Integer .MAX_VALUE ;
38
+ right [1 ] = Integer .MAX_VALUE ;
39
+ if (root .left !=null ) left = helper (root .left ); //可能为空,只有一边有节点
40
+ if (root .right !=null ) right = helper (root .right );
41
+ res_val = Math .max (Math .abs (root .val - Math .max (left [0 ], right [0 ])), res_val );
42
+ res_val = Math .max (Math .abs (root .val - Math .min (left [1 ], right [1 ])), res_val );
43
+ res [0 ] = Math .max (Math .max (left [0 ], right [0 ]), root .val ); //别忘了和root节点本身的值比
44
+ res [1 ] = Math .min (Math .min (left [1 ], right [1 ]), root .val );
45
+ return res ;
46
+ }
47
+ }
Original file line number Diff line number Diff line change
1
+ package code ;
2
+ /*
3
+ * 1027. Longest Arithmetic Sequence
4
+ * 题意:最长等差数列
5
+ * 难度:Medium
6
+ * 分类:Dynamic Programming
7
+ * 思路:还是要在每个位置上都记录一下,不是说一个大的数组就可以了。因为相同长度的等差,后续数字往上接的时候不一定接哪一个,都要保存,并不是说一定去接小的那个。
8
+ * 二维dp, 一维记录位置,一维记录差分
9
+ * Tips:很棒的题
10
+ */
11
+ public class lc1027 {
12
+ public static int longestArithSeqLength (int [] A ) {
13
+ int res = 0 ;
14
+ int [][] dp = new int [A .length ][20000 ];
15
+ for (int i = 0 ; i < A .length ; i ++) {
16
+ for (int j = 0 ; j < i ; j ++) {
17
+ int ind = A [i ]-A [j ]+10000 ;
18
+ dp [i ][ind ] = dp [j ][ind ] + 1 ;
19
+ res = Math .max (res , dp [i ][ind ]);
20
+ }
21
+ }
22
+ return res +1 ; //加1
23
+ }
24
+ }
You can’t perform that action at this time.
0 commit comments