Skip to content

Commit

Permalink
update from windows
Browse files Browse the repository at this point in the history
  • Loading branch information
miwsl committed Sep 22, 2021
1 parent 046fab8 commit db02938
Show file tree
Hide file tree
Showing 137 changed files with 9,312 additions and 173 deletions.
44 changes: 44 additions & 0 deletions algorithm/algorithmThought/dynamicProgramming.go
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)
}
108 changes: 108 additions & 0 deletions algorithm/algorithmThought/dynamicProgramming_test.go
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)
}
})
}
}
7 changes: 7 additions & 0 deletions algorithm/algorithmThought/greedyAlgorithm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package algorithmThought

//leetcode-cn上可以用到贪心算法思想的题
//122. 买卖股票的最佳时机 II
//452. 用最少数量的箭引爆气球
//455. 分发饼干
//605. 种花问题
39 changes: 34 additions & 5 deletions algorithm/sort/1_bubbleSort.go
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
}
38 changes: 35 additions & 3 deletions algorithm/sort/1_bubbleSort_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sort

import (
"fmt"
"reflect"
"testing"
)

Expand All @@ -12,19 +12,51 @@ func Test_bubbleSort(t *testing.T) {
tests := []struct {
name string
args args
want []int
}{
// TODO: Add test cases.
{
name: "case1",
args: args{
[]int{2, 1, 5, 3},
},
want: []int{1, 2, 3, 5},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := bubbleSort(tt.args.array); !reflect.DeepEqual(got, tt.want) {
t.Errorf("bubbleSort() = %v, want %v", got, tt.want)
}
})
}
}

func Test_bubbleFlagSort(t *testing.T) {
type args struct {
array []int
}
tests := []struct {
name string
args args
want []int
}{
// TODO: Add test cases.
{
name: "case1",
args: args{
[]int{2, 1, 5, 3},
},
want: []int{1, 2, 3, 5},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bubbleSort(tt.args.array)
fmt.Println(tt.args.array)
if got := bubbleFlagSort(tt.args.array); !reflect.DeepEqual(got, tt.want) {
t.Errorf("bubbleFlagSort() = %v, want %v", got, tt.want)
}
})
}
}
Empty file removed algorithm/sort/5_minHeapSort.go
Empty file.
2 changes: 1 addition & 1 deletion application/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
05_goroutinePoll.go

如果无休止的开辟Goroutine依然会出现高频率的调度Goroutine,那么依然会浪费很多上下文切换的资源,导致做无用功。所以设计一个Goroutine池限制Goroutine的开辟个数在大型并发场景还是必要的。
![](http://blog.maser.top/gogo/goroutinePool.jpg)
![](https://cdn.jsdelivr.net/gh/eaok/img/github/gogo/goroutinePool.jpg)
11 changes: 11 additions & 0 deletions demo/gin_demo/api/Dockerfile
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"]
6 changes: 6 additions & 0 deletions demo/gin_demo/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
启动:
```bash
docker-compose up mysql -d

docker-compose up api
```
79 changes: 79 additions & 0 deletions demo/gin_demo/api/api/user.go
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,
})
}
Loading

0 comments on commit db02938

Please sign in to comment.