File tree 4 files changed +87
-0
lines changed 4 files changed +87
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Move Zeroes
2
+
3
+ ### 解法一
4
+
5
+ ``` go
6
+ // 必须在原数组上操作,不能拷贝额外的数组。
7
+ // 尽量减少操作次数。
8
+
9
+ // 这道题是双指针问题无疑了,基本上数组的题都跟双指针分不开,基本上
10
+ // 双指针问题都要用到先排序,不过这个题不需要。
11
+ func moveZeroes (nums []int ) {
12
+ i,j := 0 ,0
13
+ for j < len (nums){
14
+ if nums[j] != 0 {
15
+ nums[i] = nums[j]
16
+ i++
17
+ }
18
+ j++
19
+ }
20
+ // 这个循环完毕以后,i的下下标豆薯属于非零的了,那么可以说i后面的下标都是0了,但是
21
+ // 目前还没有。因为有一些内存地址还是其它值的,
22
+
23
+ // 比如这样 [1,3,12,3,12]
24
+ // 那么我们要做的就是把i后面的索引变成0就OK了。对吧对吧。
25
+
26
+ for i < len (nums) {
27
+ nums[i] = 0
28
+ i++
29
+ }
30
+ }
31
+
32
+ ```
Original file line number Diff line number Diff line change
1
+ # Max Consecutive Ones
2
+
3
+ ### 解法一
4
+
5
+ ``` go
6
+ func findMaxConsecutiveOnes (nums []int ) int {
7
+ count := 0
8
+ // 这里的count是一个计数值,是为了最后的输出。
9
+ for i,j := 0 ,-1 ;i < len (nums);i++ {
10
+
11
+ // i ,j 在这种题中一般都是两个指针
12
+ // 即所谓的数组双指针问题,一个指针是i一个是j,i是主指针主索引,j是辅助指针。
13
+
14
+ // 在这道题中j是关键,i - j是关键,如果当nums[i] == 0的时候这个时候i - j就不能增加。所以这个时候j就要等于i,如果不是0的时候这个i - j的值就要增加所以这个时候i - j就要++ 所以这就是为什么最开始的时候j = -1
15
+ // 因为j要比i小一,才能在i - j的时候是增加的。
16
+
17
+ // 在上次是0下次是1的时候j = i 然后i ++ 了但是j没有,所以差值仍然是+1
18
+
19
+ // 如果连续几次都是nums[i] = 1 那么i一直在增加,j没有增加差值刚好是增加的次数。
20
+ if nums[i] == 0 {
21
+ j = i
22
+ }else {
23
+ if count < i - j {
24
+ count = i -j
25
+ }
26
+
27
+ }
28
+ }
29
+ return count
30
+ }
31
+ ```
Original file line number Diff line number Diff line change
1
+ # Reshape the Matrix
2
+
3
+ ### 解法一
4
+
5
+ ``` go
6
+ func matrixReshape (nums [][]int , r int , c int ) [][]int {
7
+ if len (nums) * len (nums[0 ]) != r * c || r == 0 || c == 0 || len (nums) == 0 || len (nums[0 ]) == 0 {
8
+ return nums
9
+ }
10
+
11
+ result := make ([][]int ,r)
12
+ count,col := 0 ,len (nums[0 ])
13
+ for i := range result { // 这两次的构造是按照新的数据结构去构造的。
14
+ result[i] = make ([]int ,c)
15
+ for j := range result[i] {
16
+ result[i][j] = nums[count/col][count%col] // 这里是关键
17
+ count++
18
+ }
19
+ }
20
+ return result
21
+ }
22
+ ```
Original file line number Diff line number Diff line change @@ -5,3 +5,5 @@ All leetcode questions that solved in different methods.
5
5
| question number| URL| type| result|
6
6
| :---:| :---:| :---:| :---:|
7
7
| [ 240] ( ./240.md ) | [ Search a 2D Matrix II] ( https://leetcode-cn.com/problems/search-a-2d-matrix-ii/description/ ) | array| 👌|
8
+ | [ 283] ( ./283.md ) | [ Move Zeroes] ( https://leetcode-cn.com/problems/move-zeroes/description/ ) | array| 👌|
9
+ | [ 566] ( ./566.md ) | [ Reshape the Matrix] ( https://leetcode-cn.com/problems/reshape-the-matrix/description/ ) | array| 👌|
You can’t perform that action at this time.
0 commit comments