Skip to content

Commit 322bc06

Browse files
committed
Daily Update
1 parent fb7e63a commit 322bc06

File tree

10 files changed

+355
-2
lines changed

10 files changed

+355
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ LeetCode of algorithms with golang solution(updating).
9494
[companies]: https://github.com/Blankj/awesome-java-leetcode/blob/master/Companies.md
9595

9696
[001]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/001/README.md
97-
[0007]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/007/README.md
98-
[0009]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/009/README.md
97+
[0007]: https://github.com/kylesliu/awesome-golang-leetcode/tree/master/src/0007.Reverse-Integer
98+
[0009]: https://github.com/kylesliu/awesome-golang-leetcode/tree/master/src/0009.Palindrome-Number
9999
[013]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/013/README.md
100100
[014]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/014/README.md
101101
[020]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/020/README.md

src/0000.Demo/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [1. Add Sum][title]
2+
3+
## Problem
4+
5+
### Description
6+
7+
Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
8+
9+
We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).
10+
### Example
11+
```cpp
12+
Input: [4,2,3]
13+
Output: True
14+
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
15+
16+
Input: [4,2,1]
17+
Output: False
18+
Explanation: You can't get a non-decreasing array by modify at most one element.
19+
```
20+
> Note: 如果array [i] <= array [i + 1]对于每个i(1 <= i <n)成立,我们定义一个数组是非递减的。
21+
22+
>优先把 nums[i]降为nums[i+1],这样可以减少 nums[i+1] > nums[i+2] 的风险。
23+
**Tags:**
24+
25+
26+
## 题解
27+
### 思路1
28+
在遍历数组时用 Stack 把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。
29+
30+
### 思路2
31+
32+
33+
## 结语
34+
35+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome golang leetcode][me]
36+
37+
[title]: https://leetcode.com/problems/two-sum/description/
38+
[me]: https://github.com/kylesliu/awesome-golang-leetcode

src/0000.Demo/Solution.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package Solution
2+
3+
func Solution() bool {
4+
return true
5+
}

src/0000.Demo/Solution_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Solution
2+
3+
import "testing"
4+
5+
func TestSolution(t *testing.T) {
6+
t.Run("Test-1", func(t *testing.T) {
7+
got := Solution()
8+
want := false
9+
if got != want {
10+
t.Error("GOT:", got, "WANT:", want)
11+
}
12+
})
13+
t.Run("asd", func(t *testing.T) {
14+
15+
})
16+
t.Run("Test-2", func(t *testing.T) {
17+
got := Solution()
18+
want := true
19+
if got != want {
20+
t.Error("GOT:", got, "WANT:", want)
21+
}
22+
})
23+
}

src/0001.Two-Sum/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [1. Add Sum][title]
2+
3+
## Problem
4+
5+
### Description
6+
7+
Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
8+
9+
We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).
10+
### Example
11+
```cpp
12+
Input: [4,2,3]
13+
Output: True
14+
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
15+
16+
Input: [4,2,1]
17+
Output: False
18+
Explanation: You can't get a non-decreasing array by modify at most one element.
19+
```
20+
> Note: 如果array [i] <= array [i + 1]对于每个i(1 <= i <n)成立,我们定义一个数组是非递减的。
21+
22+
>优先把 nums[i]降为nums[i+1],这样可以减少 nums[i+1] > nums[i+2] 的风险。
23+
24+
## 题解
25+
### 思路1
26+
>题意是让你从给定的数组中找到两个元素的和为指定值的两个索引,最容易的当然是循环两次,复杂度为 O(n^2),首次提交居然是 2ms,打败了 100% 的提交,谜一样的结果,之后再次提交就再也没跑到过 2ms 了。
27+
```go
28+
func twoSum(nums []int, target int) []int {
29+
if nums == nil || len(nums) < 2 {
30+
return []int{-1, -1}
31+
}
32+
33+
for k, v := range nums {
34+
for i := k + 1; i < len(nums); i++ {
35+
if v+nums[i] == target {
36+
return []int{k, i}
37+
}
38+
}
39+
}
40+
return nil
41+
}
42+
```
43+
44+
### 思路2
45+
>利用 HashMap 作为存储,键为目标值减去当前元素值,索引为值,比如 i = 0 时,此时首先要判断 nums[0] = 2 是否在 map 中,如果不存在,那么插入键值对 key = 9 - 2 = 7, value = 0,之后当 i = 1 时,此时判断 nums[1] = 7 已存在于 map 中,那么取出该 value = 0 作为第一个返回值,当前 i 作为第二个返回值,具体代码如下所示。
46+
```go
47+
func twoSum(nums []int, target int) []int {
48+
if nums == nil || len(nums) < 2 {
49+
return []int{-1, -1}
50+
}
51+
res := []int{-1, -1}
52+
53+
// MAP的KEY表示值,MAP的VAL表示nums的下标
54+
intMap := map[int]int{}
55+
for i := 0; i < len(nums); i++ {
56+
// 判断MAP中是否纯在一个Key满足 KEY + nums[i] = target
57+
// 如果满足则返回相关地址,不满足则将数组中的值PUSH到MAP
58+
if _, ok := intMap[target-nums[i]]; ok {
59+
res[0] = intMap[target-nums[i]]
60+
res[1] = i
61+
break
62+
}
63+
intMap[nums[i]] = i
64+
}
65+
return res
66+
}
67+
```
68+
69+
70+
## 结语
71+
72+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome golang leetcode][me]
73+
74+
[title]: https://leetcode.com/problems/two-sum/description/
75+
[me]: https://github.com/kylesliu/awesome-golang-leetcode

src/0001.Two-Sum/Solution.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package Solution
2+
3+
// HasMap解法
4+
func twoSum(nums []int, target int) []int {
5+
if nums == nil || len(nums) < 2 {
6+
return []int{-1, -1}
7+
}
8+
res := []int{-1, -1}
9+
10+
// MAP的KEY表示值,MAP的VAL表示nums的下标
11+
intMap := map[int]int{}
12+
for i := 0; i < len(nums); i++ {
13+
// 判断MAP中是否纯在一个Key满足 KEY + nums[i] = target
14+
// 如果满足则返回相关地址,不满足则将数组中的值PUSH到MAP
15+
if _, ok := intMap[target-nums[i]]; ok {
16+
res[0] = intMap[target-nums[i]]
17+
res[1] = i
18+
break
19+
}
20+
intMap[nums[i]] = i
21+
}
22+
return res
23+
}
24+
25+
func twoSum1(nums []int, target int) []int {
26+
if nums == nil || len(nums) < 2 {
27+
return []int{-1, -1}
28+
}
29+
30+
for k, v := range nums {
31+
for i := k + 1; i < len(nums); i++ {
32+
if v+nums[i] == target {
33+
return []int{k, i}
34+
}
35+
}
36+
}
37+
return nil
38+
}

src/0001.Two-Sum/Solution_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package Solution
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func IsEqualForSlice(a, b []int) bool {
9+
if len(a) != len(b) {
10+
fmt.Println("a")
11+
return false
12+
}
13+
// 为了和reflect.DeepEqual的结果保持一致:
14+
// []int{} != []int(nil)
15+
if (a == nil) != (b == nil) {
16+
fmt.Println("a")
17+
18+
return false
19+
}
20+
21+
// 此处的处的bounds check能够明确保证v != b[i]中的b[i]
22+
// 会出现越界错误,从而避免了b[i]中的越界检查从而提高效率
23+
// https://go101.org/article/bounds-check-elimination.html
24+
b = b[:len(a)]
25+
26+
// 循环检测每个元素是否相等
27+
for i, v := range a {
28+
if v != a[i] {
29+
return false
30+
}
31+
}
32+
return true
33+
}
34+
35+
func TestTwoSum(t *testing.T) {
36+
37+
t.Run("Test-1", func(t *testing.T) {
38+
data := []int{3, 2, 4}
39+
target := 6
40+
want := []int{1, 2}
41+
42+
got := twoSum(data, target)
43+
if !IsEqualForSlice(got, want) {
44+
t.Error("GOT:", got, " WANT:", want)
45+
}
46+
})
47+
48+
t.Run("Test-2", func(t *testing.T) {
49+
data := []int{2, 7, 11, 15}
50+
target := 9
51+
want := []int{0, 1}
52+
53+
got := twoSum(data, target)
54+
if !IsEqualForSlice(got, want) {
55+
t.Error("GOT:", got, " WANT:", want)
56+
}
57+
})
58+
59+
t.Run("Test-3", func(t *testing.T) {
60+
data := []int{7, 6, 5, 3, 2, 1, 4, 9, 10}
61+
target := 17
62+
want := []int{0, 8}
63+
64+
got := twoSum(data, target)
65+
if !IsEqualForSlice(got, want) {
66+
t.Error("GOT:", got, " WANT:", want)
67+
}
68+
})
69+
70+
}
71+
72+
func TestTwoSum1(t *testing.T) {
73+
data := []int{7, 6, 5, 3, 2, 1, 4, 9, 10}
74+
target := 17
75+
fmt.Println(twoSum1(data, target))
76+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [13. Roman to Integer][title]
2+
3+
## Description
4+
5+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
6+
7+
```
8+
Symbol Value
9+
I 1
10+
V 5
11+
X 10
12+
L 50
13+
C 100
14+
D 500
15+
M 1000
16+
```
17+
18+
For example, two is written as `II` in Roman numeral, just two one's added together. Twelve is written as, `XII`, which is simply `X` + `II`. The number twenty seven is written as `XXVII`, which is `XX` + `V` + `II`.
19+
20+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
21+
22+
- `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
23+
- `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
24+
- `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
25+
26+
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
27+
28+
**Example 1:**
29+
30+
```
31+
Input: "III"
32+
Output: 3
33+
```
34+
35+
**Example 2:**
36+
37+
```
38+
Input: "IV"
39+
Output: 4
40+
```
41+
42+
**Example 3:**
43+
44+
```
45+
Input: "IX"
46+
Output: 9
47+
```
48+
49+
**Example 4:**
50+
51+
```
52+
Input: "LVIII"
53+
Output: 58
54+
Explanation: C = 100, L = 50, XXX = 30 and III = 3.
55+
```
56+
57+
**Example 5:**
58+
59+
```
60+
Input: "MCMXCIV"
61+
Output: 1994
62+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
63+
```
64+
65+
**Tags:** Math, String
66+
67+
## 题解
68+
### 思路1
69+
在遍历数组时用 Stack 把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。
70+
71+
### 思路2
72+
73+
74+
## 结语
75+
76+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome golang leetcode][me]
77+
78+
[title]: https://leetcode.com/problems/roman-to-integer/description/
79+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package Solution
2+
3+
func romanToInt(s string) int {
4+
return 0
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package Solution
2+
3+
import "testing"
4+
5+
func TestSolution(t *testing.T) {
6+
t.Run("Test-1", func(t *testing.T) {
7+
got := romanToInt("III")
8+
want := 3
9+
if got != want {
10+
t.Error("GOT:", got, "WANT:", want)
11+
}
12+
})
13+
14+
}

0 commit comments

Comments
 (0)