File tree Expand file tree Collapse file tree 3 files changed +83
-0
lines changed
Expand file tree Collapse file tree 3 files changed +83
-0
lines changed Original file line number Diff line number Diff line change 1+ package HOT100 ;
2+
3+ import java .util .Arrays ;
4+
5+ // 最长上升子序列
6+ public class Solution300 {
7+ public int lengthOfLIS (int [] nums ) {
8+ int n = nums .length ;
9+ if (n < 2 )
10+ return n ;
11+
12+ int [] dp = new int [n ];
13+ Arrays .fill (dp , 1 );
14+ int res = 1 ;
15+
16+ for (int i = 1 ; i < n ; i ++) {
17+ for (int j = 0 ; j < i ; j ++) {
18+ if (nums [i ] > nums [j ]) {
19+ dp [i ] = Math .max (dp [i ], dp [j ] + 1 );
20+ res = Math .max (res , dp [i ]);
21+ }
22+ }
23+ }
24+ return res ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package HOT100 ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .Arrays ;
5+ import java .util .Comparator ;
6+ import java .util .List ;
7+
8+ // 根据身高重建队列
9+ public class Solution406 {
10+ public int [][] reconstructQueue (int [][] people ) {
11+ if (people .length == 0 || people [0 ].length == 0 )
12+ return new int [0 ][0 ];
13+
14+ // 身高降序,K升序
15+ Arrays .sort (people , new Comparator <int []>() {
16+ @ Override
17+ public int compare (int [] o1 , int [] o2 ) {
18+ return o1 [0 ] == o2 [0 ] ? o1 [1 ] - o2 [1 ] : o2 [0 ] - o1 [0 ];
19+ }
20+ });
21+
22+ // K为索引插入
23+ List <int []> list = new ArrayList <>();
24+ for (int [] arr : people ) {
25+ list .add (arr [1 ], arr );
26+ }
27+ return list .toArray (new int [list .size ()][]);
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package HOT100 ;
2+
3+
4+ /*
5+ class TreeNode {
6+ int val;
7+ TreeNode left;
8+ TreeNode right;
9+ TreeNode(int x) { val = x; }
10+ }
11+ */
12+
13+
14+ // 把二叉搜索树转换为累加树
15+ public class Solution538 {
16+ int num = 0 ;
17+
18+ public TreeNode convertBST (TreeNode root ) {
19+ if (root != null ) {
20+ convertBST (root .right );
21+ root .val += num ;
22+ num = root .val ;
23+ convertBST (root .left );
24+ }
25+ return root ;
26+ }
27+
28+ }
You can’t perform that action at this time.
0 commit comments