Skip to content

Commit e5bd520

Browse files
authored
Merge pull request halfrost#37 from halfrost/add_hugo
Add hugo
2 parents 5248938 + 9f47c75 commit e5bd520

File tree

64 files changed

+4149
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+4149
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
func findDisappearedNumbers(nums []int) []int {
4+
res := []int{}
5+
for _, v := range nums {
6+
if v < 0 {
7+
v = -v
8+
}
9+
if nums[v-1] > 0 {
10+
nums[v-1] = -nums[v-1]
11+
}
12+
}
13+
for i, v := range nums {
14+
if v > 0 {
15+
res = append(res, i+1)
16+
}
17+
}
18+
return res
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question448 struct {
9+
para448
10+
ans448
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para448 struct {
16+
one []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans448 struct {
22+
one []int
23+
}
24+
25+
func Test_Problem448(t *testing.T) {
26+
27+
qs := []question448{
28+
29+
question448{
30+
para448{[]int{4, 3, 2, 7, 8, 2, 3, 1}},
31+
ans448{[]int{5, 6}},
32+
},
33+
34+
question448{
35+
para448{[]int{4, 3, 2, 10, 9, 2, 3, 1, 1, 1, 1}},
36+
ans448{[]int{5, 6, 7, 8, 11}},
37+
},
38+
// 如需多个测试,可以复制上方元素。
39+
}
40+
41+
fmt.Printf("------------------------Leetcode Problem 448------------------------\n")
42+
43+
for _, q := range qs {
44+
_, p := q.ans448, q.para448
45+
fmt.Printf("【input】:%v 【output】:%v\n", p, findDisappearedNumbers(p.one))
46+
}
47+
fmt.Printf("\n\n\n")
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# [448. Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)
2+
3+
4+
## 题目
5+
6+
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
7+
8+
Find all the elements of [1, n] inclusive that do not appear in this array.
9+
10+
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
11+
12+
**Example**:
13+
14+
```
15+
Input:
16+
[4,3,2,7,8,2,3,1]
17+
18+
Output:
19+
[5,6]
20+
```
21+
22+
## 题目大意
23+
24+
给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。找到所有在 [1, n] 范围之间没有出现在数组中的数字。你能在不使用额外空间且时间复杂度为 O(n) 的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。
25+
26+
27+
28+
## 解题思路
29+
30+
- 找出 [1,n] 范围内没有出现在数组中的数字。要求不使用额外空间,并且时间复杂度为 O(n)。
31+
- 要求不能使用额外的空间,那么只能想办法在原有数组上进行修改,并且这个修改是可还原的。时间复杂度也只能允许我们一层循环。只要循环一次能标记出已经出现过的数字,这道题就可以按要求解答出来。这里笔者的标记方法是把 |nums[i]|-1 索引位置的元素标记为负数。即 nums[| nums[i] |- 1] * -1。这里需要注意的是,nums[i] 需要加绝对值,因为它可能被之前的数置为负数了,需要还原一下。最后再遍历一次数组,若当前数组元素 nums[i] 为负数,说明我们在数组中存在数字 i+1。把结果输出到最终数组里即可。
32+
33+
## 代码
34+
35+
```go
36+
func findDisappearedNumbers(nums []int) []int {
37+
res := []int{}
38+
for _, v := range nums {
39+
if v < 0 {
40+
v = -v
41+
}
42+
if nums[v-1] > 0 {
43+
nums[v-1] = -nums[v-1]
44+
}
45+
}
46+
for i, v := range nums {
47+
if v > 0 {
48+
res = append(res, i+1)
49+
}
50+
}
51+
return res
52+
}
53+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
func findMaxConsecutiveOnes(nums []int) int {
4+
maxCount, currentCount := 0, 0
5+
for _, v := range nums {
6+
if v == 1 {
7+
currentCount++
8+
} else {
9+
currentCount = 0
10+
}
11+
if currentCount > maxCount {
12+
maxCount = currentCount
13+
}
14+
}
15+
return maxCount
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question485 struct {
9+
para485
10+
ans485
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para485 struct {
16+
one []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans485 struct {
22+
one int
23+
}
24+
25+
func Test_Problem485(t *testing.T) {
26+
27+
qs := []question485{
28+
29+
question485{
30+
para485{[]int{1, 1, 0, 1, 1, 1}},
31+
ans485{3},
32+
},
33+
34+
question485{
35+
para485{[]int{1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1}},
36+
ans485{4},
37+
},
38+
// 如需多个测试,可以复制上方元素。
39+
}
40+
41+
fmt.Printf("------------------------Leetcode Problem 485------------------------\n")
42+
43+
for _, q := range qs {
44+
_, p := q.ans485, q.para485
45+
fmt.Printf("【input】:%v 【output】:%v\n", p, findMaxConsecutiveOnes(p.one))
46+
}
47+
fmt.Printf("\n\n\n")
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [485. Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/)
2+
3+
4+
## 题目
5+
6+
Given a binary array, find the maximum number of consecutive 1s in this array.
7+
8+
**Example 1**:
9+
10+
```
11+
Input: [1,1,0,1,1,1]
12+
Output: 3
13+
Explanation: The first two digits or the last three digits are consecutive 1s.
14+
The maximum number of consecutive 1s is 3.
15+
```
16+
17+
**Note**:
18+
19+
- The input array will only contain `0` and `1`.
20+
- The length of input array is a positive integer and will not exceed 10,000
21+
22+
23+
## 题目大意
24+
25+
给定一个二进制数组, 计算其中最大连续1的个数。
26+
27+
注意:
28+
29+
- 输入的数组只包含 0 和 1。
30+
- 输入数组的长度是正整数,且不超过 10,000。
31+
32+
33+
## 解题思路
34+
35+
- 给定一个二进制数组, 计算其中最大连续1的个数。
36+
- 简单题。扫一遍数组,累计 1 的个数,动态维护最大的计数,最终输出即可。
37+
38+
## 代码
39+
40+
```go
41+
func findMaxConsecutiveOnes(nums []int) int {
42+
maxCount, currentCount := 0, 0
43+
for _, v := range nums {
44+
if v == 1 {
45+
currentCount++
46+
} else {
47+
currentCount = 0
48+
}
49+
if currentCount > maxCount {
50+
maxCount = currentCount
51+
}
52+
}
53+
return maxCount
54+
}
55+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package leetcode
2+
3+
func imageSmoother(M [][]int) [][]int {
4+
res := make([][]int, len(M))
5+
for i := range M {
6+
res[i] = make([]int, len(M[0]))
7+
}
8+
for y := 0; y < len(M); y++ {
9+
for x := 0; x < len(M[0]); x++ {
10+
res[y][x] = smooth(x, y, M)
11+
}
12+
}
13+
return res
14+
}
15+
16+
func smooth(x, y int, M [][]int) int {
17+
count, sum := 1, M[y][x]
18+
// Check bottom
19+
if y+1 < len(M) {
20+
sum += M[y+1][x]
21+
count++
22+
}
23+
// Check Top
24+
if y-1 >= 0 {
25+
sum += M[y-1][x]
26+
count++
27+
}
28+
// Check left
29+
if x-1 >= 0 {
30+
sum += M[y][x-1]
31+
count++
32+
}
33+
// Check Right
34+
if x+1 < len(M[y]) {
35+
sum += M[y][x+1]
36+
count++
37+
}
38+
// Check Coners
39+
// Top Left
40+
if y-1 >= 0 && x-1 >= 0 {
41+
sum += M[y-1][x-1]
42+
count++
43+
}
44+
// Top Right
45+
if y-1 >= 0 && x+1 < len(M[0]) {
46+
sum += M[y-1][x+1]
47+
count++
48+
}
49+
// Bottom Left
50+
if y+1 < len(M) && x-1 >= 0 {
51+
sum += M[y+1][x-1]
52+
count++
53+
}
54+
//Bottom Right
55+
if y+1 < len(M) && x+1 < len(M[0]) {
56+
sum += M[y+1][x+1]
57+
count++
58+
}
59+
return sum / count
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question661 struct {
9+
para661
10+
ans661
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para661 struct {
16+
one [][]int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans661 struct {
22+
one [][]int
23+
}
24+
25+
func Test_Problem661(t *testing.T) {
26+
27+
qs := []question661{
28+
29+
question661{
30+
para661{[][]int{[]int{1, 1, 1}, []int{1, 1, 2}}},
31+
ans661{[][]int{[]int{1, 1, 1}, []int{1, 1, 1}}},
32+
},
33+
34+
question661{
35+
para661{[][]int{[]int{1, 1, 1}, []int{1, 1, 2}, []int{1, 1, 1}}},
36+
ans661{[][]int{[]int{1, 1, 1}, []int{1, 1, 1}, []int{1, 1, 1}}},
37+
},
38+
39+
question661{
40+
para661{[][]int{[]int{1, 1, 1}, []int{1, 0, 1}, []int{1, 1, 1}}},
41+
ans661{[][]int{[]int{0, 0, 0}, []int{0, 0, 0}, []int{0, 0, 0}}},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 661------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans661, q.para661
49+
fmt.Printf("【input】:%v 【output】:%v\n", p, imageSmoother(p.one))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}

0 commit comments

Comments
 (0)