Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5ada83a

Browse files
authored
Merge pull request6boris#2 from kylesliu/develop
Develop
2 parents58485eb +a99b26b commit5ada83a

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
funcconvert(sstring,numRowsint)string {
46
ifnumRows==1||numRows<=0||numRows>len(s) {
57
returns
@@ -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:=rangestrArr {
2932
res=append(res,str...)
3033
}
3134
returnstring(res)
3235
}
36+
funcconvert2(sstring,numRowsint)string {
37+
ifnumRows<=1 {
38+
returns
39+
}
40+
lenth,cycle,n:=len(s),2*numRows-2,0
41+
result:=make([]byte,lenth)
42+
fori:=0;i<numRows;i++ {
43+
forj1:=i;j1<lenth;j1=cycle+j1 {
44+
45+
n+=copy(result[n:],string(s[j1]))
46+
j2:=j1+cycle-2*i
47+
ifi!=0&&i!=numRows-1&&j2<lenth {
48+
n+=copy(result[n:],string(s[j2]))
49+
}
50+
51+
}
52+
}
53+
returnstring(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+
funccoinChange(coins []int,amountint)int {
39+
dp:=make([]int, amount+1)
40+
fori:=0; i < amount+1; i++ {
41+
dp[i] = amount +1
42+
}
43+
dp[0] =0
44+
45+
fori:=1; i <= amount; i++ {
46+
forj:=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+
funccoinChange(coins []int,amountint)int {
4+
dp:=make([]int,amount+1)
5+
fori:=0;i<amount+1;i++ {
6+
dp[i]=amount+1
7+
}
8+
dp[0]=0
9+
10+
fori:=1;i<=amount;i++ {
11+
forj:=0;j<len(coins);j++ {
12+
ifcoins[j]<=i {
13+
dp[i]=min(dp[i],dp[i-coins[j]]+1)
14+
}
15+
}
16+
}
17+
ifdp[amount]>amount {
18+
return-1
19+
}
20+
returndp[amount]
21+
}
22+
23+
funcmin(x,yint)int {
24+
ifx>y {
25+
returny
26+
}
27+
returnx
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+
funcTestSolution(t*testing.T) {
10+
//测试用例
11+
cases:= []struct {
12+
namestring
13+
inputs []int
14+
amountint
15+
expectint
16+
}{
17+
{"TestCase", []int{1,2,5},11,3},
18+
{"TestCase", []int{2},3,-1},
19+
}
20+
21+
//开始测试
22+
fori,c:=rangecases {
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+
funcBenchmarkSolution(b*testing.B) {
35+
36+
}
37+
38+
//使用案列
39+
funcExampleSolution() {
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+
funcfindMinArrowShots(points [][]int)int {
8+
iflen(points)==0 {
9+
return0
10+
}
11+
sort.Sort(ByLen(points))
12+
13+
res:=0
14+
end:=-1<<31-1
15+
for_,interval:=rangepoints {
16+
ifinterval[0]>end {
17+
res+=1
18+
end=interval[1]
19+
}
20+
}
21+
returnres
22+
}
23+
24+
typeByLen [][]int
25+
26+
func (aByLen)Len()int {returnlen(a) }
27+
func (aByLen)Less(i,jint)bool {returna[i][1]<a[j][1] }
28+
func (aByLen)Swap(i,jint) {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+
funcTestSolution(t*testing.T) {
10+
//测试用例
11+
cases:= []struct {
12+
namestring
13+
inputs [][]int
14+
expectint
15+
}{
16+
{"TestCase", [][]int{
17+
{10,16},
18+
{2,8},
19+
{1,6},
20+
{7,12},
21+
},2},
22+
}
23+
24+
//开始测试
25+
fori,c:=rangecases {
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+
funcBenchmarkSolution(b*testing.B) {
38+
39+
}
40+
41+
//使用案列
42+
funcExampleSolution() {
43+
44+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp