Skip to content

Commit 5ada83a

Browse files
authored
Merge pull request 6boris#2 from kylesliu/develop
Develop
2 parents 58485eb + a99b26b commit 5ada83a

File tree

8 files changed

+291
-0
lines changed

8 files changed

+291
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ LeetCode of algorithms with golang solution(updating:smiley:).
3535

3636
[![Stargazers over time](https://starcharts.herokuapp.com/kylesliu/awesome-golang-leetcode.svg)](https://starcharts.herokuapp.com/kylesliu/awesome-golang-leetcode)
3737

38+
## Community
39+
40+
* [leetbook](https://github.com/hk029/leetbook) 某位大佬写的Leetcode题解,不过已经不更新了
41+
* [LeetCode-in-Go](https://github.com/aQuaYi/LeetCode-in-Go) 某位算法大佬的Golang题解
42+
3843
## Contributors
3944

4045
Thanks goes to these wonderful people ([emoji key](https://github.com/all-contributors/all-contributors#emoji-key)):

src/0006.ZigZag-Conversion/Solution.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package Solution
22

3+
import "fmt"
4+
35
func convert(s string, numRows int) string {
46
if numRows == 1 || numRows <= 0 || numRows > len(s) {
57
return s
@@ -23,10 +25,30 @@ func convert(s string, numRows int) string {
2325
}
2426
}
2527
}
28+
fmt.Println(strArr)
2629
res := make([]byte, 0)
2730

2831
for _, str := range strArr {
2932
res = append(res, str...)
3033
}
3134
return string(res)
3235
}
36+
func convert2(s string, numRows int) string {
37+
if numRows <= 1 {
38+
return s
39+
}
40+
lenth, cycle, n := len(s), 2*numRows-2, 0
41+
result := make([]byte, lenth)
42+
for i := 0; i < numRows; i++ {
43+
for j1 := i; j1 < lenth; j1 = cycle + j1 {
44+
45+
n += copy(result[n:], string(s[j1]))
46+
j2 := j1 + cycle - 2*i
47+
if i != 0 && i != numRows-1 && j2 < lenth {
48+
n += copy(result[n:], string(s[j2]))
49+
}
50+
51+
}
52+
}
53+
return string(result)
54+
}

src/0322.Coin-Change/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [322. Coin Change][title]
2+
3+
## Description
4+
5+
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
6+
7+
**Example 1:**
8+
9+
```
10+
Input: coins = [1, 2, 5], amount = 11
11+
Output: 3
12+
Explanation: 11 = 5 + 5 + 1
13+
```
14+
15+
**Example 2:**
16+
17+
```
18+
Input: coins = [2], amount = 3
19+
Output: -1
20+
```
21+
22+
**Tags:** Math, String
23+
24+
## 题意
25+
> 给定 nn 种不同硬币的面值,以及需要凑出的总面值 totaltotal。请写一个函数,求最少需要多少硬币,可以凑出 totaltotal 的钱。
26+
如果不存在任何一种拼凑方案,则返回-1。
27+
28+
29+
30+
## 题解
31+
32+
### 动态规划
33+
> 状态表示:
34+
- dp[i]表示凑出 ii 价值的钱,最少需要多少个硬币。
35+
- 第i种硬币 dp[i] = min(dp[i], min(dp[i - coins[i]] ) + 1)
36+
37+
```go
38+
func coinChange(coins []int, amount int) int {
39+
dp := make([]int, amount+1)
40+
for i := 0; i < amount+1; i++ {
41+
dp[i] = amount + 1
42+
}
43+
dp[0] = 0
44+
45+
for i := 1; i <= amount; i++ {
46+
for j := 0; j < len(coins); j++ {
47+
if coins[j] <= i {
48+
dp[i] = min(dp[i], dp[i-coins[j]]+1)
49+
}
50+
}
51+
}
52+
if dp[amount] > amount {
53+
return -1
54+
}
55+
return dp[amount]
56+
}
57+
```
58+
59+
### 思路2
60+
> 思路2
61+
```go
62+
63+
```
64+
65+
## 结语
66+
67+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
68+
69+
[title]: https://leetcode.com/problems/two-sum/description/
70+
[me]: https://github.com/kylesliu/awesome-golang-leetcode

src/0322.Coin-Change/Solution.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Solution
2+
3+
func coinChange(coins []int, amount int) int {
4+
dp := make([]int, amount+1)
5+
for i := 0; i < amount+1; i++ {
6+
dp[i] = amount + 1
7+
}
8+
dp[0] = 0
9+
10+
for i := 1; i <= amount; i++ {
11+
for j := 0; j < len(coins); j++ {
12+
if coins[j] <= i {
13+
dp[i] = min(dp[i], dp[i-coins[j]]+1)
14+
}
15+
}
16+
}
17+
if dp[amount] > amount {
18+
return -1
19+
}
20+
return dp[amount]
21+
}
22+
23+
func min(x, y int) int {
24+
if x > y {
25+
return y
26+
}
27+
return x
28+
}

src/0322.Coin-Change/Solution_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs []int
14+
amount int
15+
expect int
16+
}{
17+
{"TestCase", []int{1, 2, 5}, 11, 3},
18+
{"TestCase", []int{2}, 3, -1},
19+
}
20+
21+
// 开始测试
22+
for i, c := range cases {
23+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24+
got := coinChange(c.inputs, c.amount)
25+
if !reflect.DeepEqual(got, c.expect) {
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27+
c.expect, got, c.inputs)
28+
}
29+
})
30+
}
31+
}
32+
33+
// 压力测试
34+
func BenchmarkSolution(b *testing.B) {
35+
36+
}
37+
38+
// 使用案列
39+
func ExampleSolution() {
40+
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# [452. Minimum Number of Arrows to Burst Balloons][title]
2+
3+
## Description
4+
5+
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.
6+
7+
An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.
8+
**Example 1:**
9+
10+
```
11+
Input:
12+
[[10,16], [2,8], [1,6], [7,12]]
13+
14+
Output:
15+
2
16+
Explanation:
17+
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).
18+
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: a = "1010", b = "1011"
25+
Output: "10101"
26+
```
27+
28+
**Tags:** Math, String
29+
30+
## 题意
31+
> 求2数之和
32+
33+
## 题解
34+
35+
### 思路1
36+
> 。。。。
37+
38+
```go
39+
40+
```
41+
42+
### 思路2
43+
> 思路2
44+
```go
45+
46+
```
47+
48+
## 结语
49+
50+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
51+
52+
[title]: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/
53+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Solution
2+
3+
import (
4+
"sort"
5+
)
6+
7+
func findMinArrowShots(points [][]int) int {
8+
if len(points) == 0 {
9+
return 0
10+
}
11+
sort.Sort(ByLen(points))
12+
13+
res := 0
14+
end := -1<<31 - 1
15+
for _, interval := range points {
16+
if interval[0] > end {
17+
res += 1
18+
end = interval[1]
19+
}
20+
}
21+
return res
22+
}
23+
24+
type ByLen [][]int
25+
26+
func (a ByLen) Len() int { return len(a) }
27+
func (a ByLen) Less(i, j int) bool { return a[i][1] < a[j][1] }
28+
func (a ByLen) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs [][]int
14+
expect int
15+
}{
16+
{"TestCase", [][]int{
17+
{10, 16},
18+
{2, 8},
19+
{1, 6},
20+
{7, 12},
21+
}, 2},
22+
}
23+
24+
// 开始测试
25+
for i, c := range cases {
26+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
27+
got := findMinArrowShots(c.inputs)
28+
if !reflect.DeepEqual(got, c.expect) {
29+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
30+
c.expect, got, c.inputs)
31+
}
32+
})
33+
}
34+
}
35+
36+
// 压力测试
37+
func BenchmarkSolution(b *testing.B) {
38+
39+
}
40+
41+
// 使用案列
42+
func ExampleSolution() {
43+
44+
}

0 commit comments

Comments
 (0)