-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
miwsl
committed
Sep 22, 2021
1 parent
046fab8
commit db02938
Showing
137 changed files
with
9,312 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package algorithmThought | ||
|
||
//leetcode-cn上可以用到动态规划思想的题 | ||
//10. 正则表达式匹配 | ||
//62. 不同路径 | ||
//70. 爬楼梯 | ||
|
||
// FibonacciDP 返回斐波那契数列中第n个数 | ||
// 动态规划实现 | ||
func FibonacciDP(n int) int { | ||
dp := make([]int, n) // 用于缓存以往结果,以便复用 | ||
dp[0] = 1 | ||
dp[1] = 1 | ||
|
||
// 按顺序从小往大算 | ||
for i := 2; i < n; i++ { | ||
// 使用状态转移方程,同时复用以往结果 | ||
dp[i] = dp[i-1] + dp[i-2] | ||
} | ||
|
||
return dp[n-1] | ||
} | ||
|
||
// FibonacciDP 返回斐波那契数列中第n个数 | ||
// 动态规划实现优化版 | ||
func FibonacciDPOptimize(n int) int { | ||
dp := []int{1, 1} | ||
|
||
for i := 2; i < n; i++ { | ||
dp[0], dp[1] = dp[1], dp[0]+dp[1] | ||
} | ||
|
||
return dp[1] | ||
} | ||
|
||
// FibonacciRE 返回斐波那契数列中第n个数 | ||
// 递归实现,为了对比动态规划 | ||
func FibonacciRE(n int) int { | ||
if n < 2 { | ||
return n | ||
} | ||
|
||
return FibonacciRE(n-1) + FibonacciRE(n-2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package algorithmThought | ||
|
||
import "testing" | ||
|
||
func TestFibonacciDP(t *testing.T) { | ||
type args struct { | ||
n int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want int | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "data1", | ||
args: args{3}, | ||
want: 2, | ||
}, | ||
{ | ||
name: "data2", | ||
args: args{4}, | ||
want: 3, | ||
}, | ||
{ | ||
name: "data3", | ||
args: args{6}, | ||
want: 8, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := FibonacciDP(tt.args.n); got != tt.want { | ||
t.Errorf("FibonacciDP() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFibonacciDPOptimize(t *testing.T) { | ||
type args struct { | ||
n int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want int | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "data1", | ||
args: args{3}, | ||
want: 2, | ||
}, | ||
{ | ||
name: "data2", | ||
args: args{4}, | ||
want: 3, | ||
}, | ||
{ | ||
name: "data3", | ||
args: args{6}, | ||
want: 8, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := FibonacciDPOptimize(tt.args.n); got != tt.want { | ||
t.Errorf("FibonacciDPOptimize() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFibonacciRE(t *testing.T) { | ||
type args struct { | ||
n int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want int | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "data1", | ||
args: args{3}, | ||
want: 2, | ||
}, | ||
{ | ||
name: "data2", | ||
args: args{4}, | ||
want: 3, | ||
}, | ||
{ | ||
name: "data3", | ||
args: args{6}, | ||
want: 8, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := FibonacciRE(tt.args.n); got != tt.want { | ||
t.Errorf("FibonacciRE() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package algorithmThought | ||
|
||
//leetcode-cn上可以用到贪心算法思想的题 | ||
//122. 买卖股票的最佳时机 II | ||
//452. 用最少数量的箭引爆气球 | ||
//455. 分发饼干 | ||
//605. 种花问题 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,43 @@ | ||
package sort | ||
|
||
//bobbleSort 冒泡排序 | ||
func bubbleSort(array []int) { | ||
// bobbleSort 冒泡排序 | ||
func bubbleSort(array []int) []int { | ||
if len(array) == 0 { | ||
return array | ||
} | ||
|
||
for i := 0; i < len(array)-1; i++ { | ||
for j := 0; j < len(array)-1-i; j++ { | ||
// 升序排序 | ||
if array[j] > array[j+1] { | ||
tmp := array[j] | ||
array[j] = array[j+1] | ||
array[j+1] = tmp | ||
array[j], array[j+1] = array[j+1], array[j] | ||
} | ||
} | ||
} | ||
|
||
return array | ||
} | ||
|
||
// bobbleFlagSort 冒泡排序,加如flag改进 | ||
func bubbleFlagSort(array []int) []int { | ||
if len(array) == 0 { | ||
return array | ||
} | ||
|
||
flag := 0 | ||
for i := 0; i < len(array)-1; i++ { | ||
flag = 0 | ||
for j := 0; j < len(array)-1-i; j++ { | ||
// 升序排序 | ||
if array[j] > array[j+1] { | ||
array[j], array[j+1] = array[j+1], array[j] | ||
flag = 1 | ||
} | ||
} | ||
if flag == 0 { | ||
break | ||
} | ||
} | ||
|
||
return array | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM golang:latest as build-env | ||
ENV GOPROXY=https://goproxy.cn,direct CGO_ENABLED=0 | ||
WORKDIR /home/docker | ||
COPY . . | ||
RUN go build -o api . | ||
|
||
FROM alpine:latest | ||
LABEL MAINTAINER="kcoewoys" | ||
EXPOSE 8080 | ||
COPY --from=build-env /home/docker/api /home/docker/ | ||
CMD ["/home/docker/api"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
启动: | ||
```bash | ||
docker-compose up mysql -d | ||
|
||
docker-compose up api | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package api | ||
|
||
import ( | ||
"api/models" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
//index | ||
func IndexUsers(c *gin.Context) { | ||
c.String(http.StatusOK, "It works") | ||
} | ||
|
||
//增加一条记录 | ||
func AddUsers(c *gin.Context) { | ||
name := c.Request.FormValue("name") | ||
telephone := c.Request.FormValue("telephone") | ||
person := models.Person{ | ||
Name: name, | ||
Telephone: telephone, | ||
} | ||
id := person.Create() | ||
msg := fmt.Sprintf("insert successful %d", id) | ||
c.JSON(http.StatusOK, gin.H{ | ||
"msg": msg, | ||
}) | ||
} | ||
|
||
//获得一条记录 | ||
func GetOne(c *gin.Context) { | ||
ids := c.Param("id") | ||
id, _ := strconv.Atoi(ids) | ||
p := models.Person{ | ||
Id: id, | ||
} | ||
rs, _ := p.GetRow() | ||
c.JSON(http.StatusOK, gin.H{ | ||
"result": rs, | ||
}) | ||
} | ||
|
||
//获得所有记录 | ||
func GetAll(c *gin.Context) { | ||
p := models.Person{} | ||
rs, _ := p.GetRows() | ||
c.JSON(http.StatusOK, gin.H{ | ||
"list": rs, | ||
}) | ||
} | ||
|
||
//更新telephone | ||
func UpdateUser(c *gin.Context) { | ||
ids := c.Request.FormValue("id") | ||
id, _ := strconv.Atoi(ids) | ||
telephone := c.Request.FormValue("telephone") | ||
person := models.Person{ | ||
Id: id, | ||
Telephone: telephone, | ||
} | ||
row := person.Update() | ||
msg := fmt.Sprintf("updated successful %d", row) | ||
c.JSON(http.StatusOK, gin.H{ | ||
"msg": msg, | ||
}) | ||
} | ||
|
||
//删除一条记录 | ||
func DelUser(c *gin.Context) { | ||
ids := c.Request.FormValue("id") | ||
id, _ := strconv.Atoi(ids) | ||
row := models.Delete(id) | ||
msg := fmt.Sprintf("delete successful %d", row) | ||
c.JSON(http.StatusOK, gin.H{ | ||
"msg": msg, | ||
}) | ||
} |
Oops, something went wrong.