File tree Expand file tree Collapse file tree 4 files changed +150
-0
lines changed
src/main/java/com/chen/algorithm/study Expand file tree Collapse file tree 4 files changed +150
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .study .test240 ;
2+
3+ /**
4+ * https://leetcode-cn.com/problems/search-a-2d-matrix-ii/solution/er-fen-fa-pai-chu-fa-python-dai-ma-java-dai-ma-by-/
5+ *
6+ * @author : chen weijie
7+ * @Date: 2019-12-22 14:26
8+ */
9+ public class Solution {
10+
11+
12+ /**
13+ * 从左下角开始
14+ * <p>
15+ * 如果当前数比目标元素小,当前列就不可能存在目标值,“指针”就向右移一格(纵坐标加 11);
16+ * 如果当前数比目标元素大,当前行就不可能存在目标值,“指针”就向上移一格(横坐标减 11)。
17+ *
18+ * @param matrix
19+ * @param target
20+ * @return
21+ */
22+ public boolean searchMatrix (int [][] matrix , int target ) {
23+
24+ int rows = matrix .length ;
25+ if (rows == 0 ) {
26+ return false ;
27+ }
28+
29+ int col = matrix [0 ].length ;
30+
31+ if (col == 0 ) {
32+ return false ;
33+ }
34+
35+ int x = rows - 1 ;
36+ int y = 0 ;
37+ int value ;
38+
39+ while (x >= 0 && y < col ) {
40+ value = matrix [x ][y ];
41+ if (value > target ) {
42+ x --;
43+ } else if (value < target ) {
44+ y ++;
45+ } else {
46+ return true ;
47+ }
48+ }
49+
50+ return false ;
51+ }
52+
53+
54+ }
Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .study .test279 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2019-12-22 15:13
6+ */
7+ public class Solution {
8+
9+
10+ int [] memo ;
11+
12+ public int numSquares (int n ) {
13+ memo = new int [n + 1 ];
14+
15+ return numSqu (n );
16+
17+ }
18+
19+
20+ public int numSqu (int n ) {
21+ if (memo [n ] != 0 ) {
22+ return memo [n ];
23+ }
24+
25+ int val = (int ) Math .sqrt (n );
26+ if (val * val == n ) {
27+ return memo [0 ] = 1 ;
28+ }
29+ int res = Integer .MAX_VALUE ;
30+ for (int i = 1 ; i * i < n ; i ++) {
31+ res = Math .min (res , numSqu (n - i * i ) + 1 );
32+ }
33+
34+ return memo [n ] = res ;
35+ }
36+
37+ }
Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .study .test287 ;
2+
3+ import java .util .Arrays ;
4+
5+ /**
6+ * @author : chen weijie
7+ * @Date: 2019-12-22 16:22
8+ */
9+ public class Solution {
10+
11+
12+ public int findDuplicate (int [] nums ) {
13+
14+ Arrays .sort (nums );
15+
16+ for (int i = 1 ; i < nums .length ; i ++) {
17+ if (nums [i ] == nums [i - 1 ]) {
18+ return nums [i ];
19+ }
20+ }
21+ return -1 ;
22+ }
23+
24+
25+ }
Original file line number Diff line number Diff line change 1+ package com .chen .algorithm .study .test300 ;
2+
3+ /**
4+ * @author : chen weijie
5+ * @Date: 2019-12-22 16:28
6+ */
7+ public class Solution {
8+
9+ public int lengthOfLIST (int [] nums ) {
10+
11+ if (nums .length == 0 ) {
12+ return 0 ;
13+ }
14+
15+
16+ int max = 1 ;
17+ int length = 1 ;
18+ int temp = nums [0 ];
19+ for (int i = 1 ; i < nums .length ; i ++) {
20+ if (nums [i ] > temp ) {
21+ length ++;
22+ max = Math .max (length , max );
23+ temp = nums [i ];
24+ } else {
25+ temp = nums [i - 1 ];
26+ length = 1 ;
27+ }
28+ }
29+
30+
31+ return max ;
32+ }
33+
34+ }
You can’t perform that action at this time.
0 commit comments