File tree 7 files changed +84
-47
lines changed
7 files changed +84
-47
lines changed Original file line number Diff line number Diff line change @@ -47,11 +47,9 @@ class Solution:
47
47
``` java
48
48
class Solution {
49
49
public int findRepeatNumber (int [] nums ) {
50
- for (int i = 0 , len = nums. length; i < len; ++ i) {
51
- while (i != nums[i]) {
52
- if (nums[i] == nums[nums[i]]) {
53
- return nums[i];
54
- }
50
+ for (int i = 0 , n = nums. length; i < n; ++ i) {
51
+ while (nums[i] != i) {
52
+ if (nums[i] == nums[nums[i]]) return nums[i];
55
53
swap(nums, i, nums[i]);
56
54
}
57
55
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int findRepeatNumber (int [] nums ) {
3
- for (int i = 0 , len = nums .length ; i < len ; ++i ) {
4
- while (i != nums [i ]) {
5
- if (nums [i ] == nums [nums [i ]]) {
6
- return nums [i ];
7
- }
3
+ for (int i = 0 , n = nums .length ; i < n ; ++i ) {
4
+ while (nums [i ] != i ) {
5
+ if (nums [i ] == nums [nums [i ]]) return nums [i ];
8
6
swap (nums , i , nums [i ]);
9
7
}
10
8
}
Original file line number Diff line number Diff line change @@ -59,20 +59,13 @@ class Solution:
59
59
``` java
60
60
class Solution {
61
61
public boolean findNumberIn2DArray (int [][] matrix , int target ) {
62
- if (matrix == null || matrix. length == 0 || matrix[0 ] == null || matrix[0 ]. length == 0 ) {
63
- return false ;
64
- }
65
- int rows = matrix. length, cols = matrix[0 ]. length;
66
- int i = rows - 1 , j = 0 ;
67
- while (i >= 0 && j < cols) {
68
- if (matrix[i][j] == target) {
69
- return true ;
70
- }
71
- if (matrix[i][j] > target) {
72
- -- i;
73
- } else {
74
- ++ j;
75
- }
62
+ int m, n;
63
+ if (matrix == null || (m = matrix. length) == 0 || matrix[0 ] == null || (n = matrix[0 ]. length) == 0 ) return false ;
64
+ int i = 0 , j = n - 1 ;
65
+ while (i < m && j >= 0 ) {
66
+ if (matrix[i][j] == target) return true ;
67
+ if (matrix[i][j] > target) -- j;
68
+ else ++ i;
76
69
}
77
70
return false ;
78
71
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public boolean findNumberIn2DArray (int [][] matrix , int target ) {
3
- if (matrix == null || matrix .length == 0 || matrix [0 ] == null || matrix [0 ].length == 0 ) {
4
- return false ;
5
- }
6
- int rows = matrix .length , cols = matrix [0 ].length ;
7
- int i = rows - 1 , j = 0 ;
8
- while (i >= 0 && j < cols ) {
9
- if (matrix [i ][j ] == target ) {
10
- return true ;
11
- }
12
- if (matrix [i ][j ] > target ) {
13
- --i ;
14
- } else {
15
- ++j ;
16
- }
3
+ int m , n ;
4
+ if (matrix == null || (m = matrix .length ) == 0 || matrix [0 ] == null || (n = matrix [0 ].length ) == 0 ) return false ;
5
+ int i = 0 , j = n - 1 ;
6
+ while (i < m && j >= 0 ) {
7
+ if (matrix [i ][j ] == target ) return true ;
8
+ if (matrix [i ][j ] > target ) --j ;
9
+ else ++i ;
17
10
}
18
11
return false ;
19
12
}
Original file line number Diff line number Diff line change @@ -31,6 +31,8 @@ class Solution:
31
31
32
32
### ** Java**
33
33
34
+ - 使用 replace:
35
+
34
36
``` java
35
37
class Solution {
36
38
public String replaceSpace (String s ) {
@@ -39,6 +41,21 @@ class Solution {
39
41
}
40
42
```
41
43
44
+ - 使用 StringBuilder:
45
+
46
+ ``` java
47
+ class Solution {
48
+ public String replaceSpace (String s ) {
49
+ StringBuilder sb = new StringBuilder ();
50
+ char [] chars = s. toCharArray();
51
+ for (char c : chars) {
52
+ sb. append(c == ' ' ? " %20" : c);
53
+ }
54
+ return sb. toString();
55
+ }
56
+ }
57
+ ```
58
+
42
59
### ** JavaScript**
43
60
44
61
``` js
Original file line number Diff line number Diff line change 17
17
18
18
## 解法
19
19
20
- 栈实现。
20
+ 栈实现。或者其它方式,见题解。
21
21
22
22
<!-- tabs:start -->
23
23
@@ -41,6 +41,8 @@ class Solution:
41
41
42
42
### ** Java**
43
43
44
+ - 栈实现:
45
+
44
46
``` java
45
47
/**
46
48
* Definition for singly-linked list.
@@ -67,6 +69,38 @@ class Solution {
67
69
}
68
70
```
69
71
72
+ - 先计算链表长度 n,然后创建一个长度为 n 的结果数组。最后遍历链表,依次将节点值存放在数组上(从后往前)。
73
+
74
+ ``` java
75
+ /**
76
+ * Definition for singly-linked list.
77
+ * public class ListNode {
78
+ * int val;
79
+ * ListNode next;
80
+ * ListNode(int x) { val = x; }
81
+ * }
82
+ */
83
+ class Solution {
84
+ public int [] reversePrint (ListNode head ) {
85
+ if (head == null ) return new int []{};
86
+ // 计算链表长度n
87
+ int n = 0 ;
88
+ ListNode cur = head;
89
+ while (cur != null ) {
90
+ ++ n;
91
+ cur = cur. next;
92
+ }
93
+ int [] res = new int [n];
94
+ cur = head;
95
+ while (cur != null ) {
96
+ res[-- n] = cur. val;
97
+ cur = cur. next;
98
+ }
99
+ return res;
100
+ }
101
+ }
102
+ ```
103
+
70
104
### ** Go**
71
105
72
106
``` go
Original file line number Diff line number Diff line change 8
8
*/
9
9
class Solution {
10
10
public int [] reversePrint (ListNode head ) {
11
- Stack <Integer > s = new Stack <>();
12
- while (head != null ) {
13
- s .push (head .val );
14
- head = head .next ;
11
+ if (head == null ) return new int []{};
12
+ // 计算链表长度n
13
+ int n = 0 ;
14
+ ListNode cur = head ;
15
+ while (cur != null ) {
16
+ ++n ;
17
+ cur = cur .next ;
15
18
}
16
- int [] res = new int [s .size ()];
17
- int i = 0 ;
18
- while (!s .isEmpty ()) {
19
- res [i ++] = s .pop ();
19
+ int [] res = new int [n ];
20
+ cur = head ;
21
+ while (cur != null ) {
22
+ res [--n ] = cur .val ;
23
+ cur = cur .next ;
20
24
}
21
25
return res ;
22
26
}
You can’t perform that action at this time.
0 commit comments