Skip to content

Commit 407fdbc

Browse files
JackieHooJackieHoo
JackieHoo
authored and
JackieHoo
committed
add
1 parent c952817 commit 407fdbc

File tree

7 files changed

+148
-0
lines changed

7 files changed

+148
-0
lines changed

287.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Find the Duplicate Number
2+
### 解法一
3+
快慢指针
4+
5+
寻找环
6+
```go
7+
func findDuplicate(nums []int) int { // 这种写法是标准的链表双指针寻找环的写法。
8+
low,fast := nums[0],nums[nums[0]]// 首先制定固有的速率,low是nums[low]的速度,fast是二倍于它的速度就是 nums[nums[0]]
9+
for low != fast {
10+
low = nums[low] // 按照不同的速度(存在倍数关系),如果这两者能相遇,就只有一个原因就是因为存在环。
11+
fast = nums[nums[fast]]
12+
}
13+
// 但是他们相遇的地方不一定是入口处,有可能是环的内部的某个元素,这个元素叫做相遇点。
14+
low = 0// 然后这一步我门要寻找这个环的入口,将low设置为0就是从头开始走,然后fast在相遇点。这个时候把他们的速度调成一样。
15+
// 即: low = nums[low] fast = nums[fast] 这样速度就一样了,(都是一次,之前速度不同的时候fast直接两次)
16+
for low != fast {
17+
low = nums[low]
18+
fast = nums[fast]
19+
}
20+
return low
21+
}
22+
```

565.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Array Nesting
2+
### 解法一
3+
```go
4+
func arrayNesting(nums []int) int {
5+
max := 0
6+
for i := range nums {
7+
p := 1
8+
for nums[i] != i {
9+
nums[i],nums[nums[i]] = nums[nums[i]],nums[i]
10+
// 当 nums[i] = i 就可以退出了,
11+
//然后为什么用吧0 - n的下标作为起始点呢
12+
//就是因为题目中并没有说谁是第一个index,所以要把所有的情况都考虑到。
13+
p++
14+
}
15+
if max < p {
16+
max = p
17+
}
18+
}
19+
return max
20+
}
21+
```

667.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Beautiful Arrangement II
2+
### 解法一
3+
```go
4+
func constructArray(n int, k int) []int {
5+
让 := make([]int,0,n)
6+
7+
i,j := 1,n
8+
for i <= j {
9+
if k %2 ==1 {
10+
让 = append(让,i)
11+
i++
12+
}else {
13+
让 = append(让,j)
14+
j--
15+
}
16+
if k >1 {
17+
k--
18+
}
19+
}
20+
}
21+
```

697.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Degree of an Array
2+
### 解法一
3+
```go
4+
func findShortestSubArray(nums []int) int {
5+
// 使用两个map来寻找这个值,
6+
// 首先map1 是计算的len长度,map2 是计算的首次出现的index
7+
Count,minLength := make(map[int]int),make(map[int]int)
8+
l := len(nums)
9+
maxLen := 1
10+
if l < 2 { // 边界判断。
11+
return l
12+
}
13+
for i,v := range nums {
14+
Count[v]++ // 是计算各个v的出现次数的
15+
if Count[v] == 1 { // 记录第一次出现的i
16+
minLength[v] = i
17+
}else {
18+
dd := i - minLength[v] + 1 // 然后每次都记录长度
19+
if maxLen < Count[v] || maxLen == Count[v] && l > dd {
20+
l = dd // 这个l就是一个记录index差的的指针。
21+
maxLen = Count[v] // 这个maxlen就是总长度最大的指针。
22+
// 这是两个必不可少的指针。 这里就是一边遍历一边比较。
23+
}
24+
}
25+
}
26+
if len(Count) == len(nums) { // 当没有重复的时候返回1,因为map的len等于这个num了嘛。
27+
return 1
28+
}
29+
return l
30+
}
31+
```

766.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Toeplitz Matrix
2+
### 解法一
3+
```go
4+
func isToeplitzMatrix(matrix [][]int) bool {
5+
// 判断边界
6+
if len(matrix) == 0 {
7+
return false
8+
}
9+
// 遍历这个二元数组,当不是右侧和下侧的边界的时候,只要x v != x+1 v+1 就是false的。
10+
for k, v:= range matrix {
11+
for k1 := range v {
12+
if k <len(matrix)-1 && k1 < len(v)-1 {
13+
if matrix[k][k1] != matrix[k+1][k1+1] {
14+
return false
15+
}
16+
}
17+
}
18+
}
19+
return true
20+
}
21+
```

769.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Max Chunks To Make Sorted
2+
3+
### 解法一
4+
5+
```go
6+
func maxChunksToSorted(arr []int) int {
7+
// 处理边界条件
8+
if len(arr) == 0 {
9+
return 0
10+
}
11+
// 设置最右边的值
12+
right := arr[0]
13+
// 设置计数指针
14+
num := 0
15+
for i := 0;i < len(arr);i++ {
16+
// 寻找right和arr[i]的最大值(就是right)
17+
right = int(math.Max(float64(arr[i]),float64(right)))
18+
// 这个条件就是当right等于i的时候就是OK的
19+
// 如果不等于那么分配好的再分配也是无法排序的。
20+
if right == i {
21+
num ++
22+
}
23+
}
24+
return num
25+
}
26+
```

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ All leetcode questions that solved in different methods.
66
|:---:|:---:|:---:|:---:|
77
|[240](./240.md)|[Search a 2D Matrix II](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/description/)|array|👌|
88
|[283](./283.md)|[Move Zeroes](https://leetcode-cn.com/problems/move-zeroes/description/)|array|👌|
9+
|[287](./287.md)|[Find the Duplicate Number](https://leetcode-cn.com/problems/find-the-duplicate-number/description/)|array|👌|
910
|[378](./378.md)|[Kth Smallest Element in a Sorted Matrix](https://leetcode-cn.com/problems/kth-smallest-element-in-a-sorted-matrix/description/)|array|👌|
1011
|[485](./485.md)|[Max Consecutive Ones](https://leetcode-cn.com/problems/max-consecutive-ones/description/)|array|👌|
12+
|[565](./565.md)|[Array Nesting](https://leetcode-cn.com/problems/array-nesting/description/)|array|👌|
1113
|[645](./645.md)|[Set Mismatch](https://leetcode-cn.com/problems/set-mismatch/description/)|array|👌|
14+
|[667](./667.md)|[Beautiful Arrangement II](https://leetcode-cn.com/problems/beautiful-arrangement-ii/description/)|array|👌|
15+
|[697](./697.md)|[Degree of an Array](https://leetcode-cn.com/problems/degree-of-an-array/description/)|array|👌|
16+
|[766](./766.md)|[Toeplitz Matrix](https://leetcode-cn.com/problems/toeplitz-matrix/)|array|👌|
17+
|[769](./769.md)|[Max Chunks To Make Sorted](https://leetcode-cn.com/problems/max-chunks-to-make-sorted/description/)|array|👌|

0 commit comments

Comments
 (0)