From 4219a998278dfe268dc166566e982b84405fbc55 Mon Sep 17 00:00:00 2001 From: Bin Liang Date: Thu, 9 Jul 2020 22:10:36 +0800 Subject: [PATCH] feat: add golang solution for lcof problem 12 13 14I --- .../README.md" | 44 +++++++++++++++++++ .../Solution.go" | 36 +++++++++++++++ .../README.md" | 24 ++++++++++ .../Solution.go" | 16 +++++++ .../README.md" | 21 +++++++++ .../Solution.go" | 11 +++++ 6 files changed, 152 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.go" create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.go" create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/Solution.go" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" index 0e7d5cbb6db71..41c569c2ba1af 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" @@ -138,7 +138,51 @@ var exist = function(board, word) { }; ``` +### Go + +```go +func exist(board [][]byte, word string) bool { + if len(board) == 0 { + return false + } + //标记数组 + isVisited := make([][]bool,len(board)) + for i := 0; i < len(board); i++ { + isVisited[i] = make([]bool, len(board[0])) + } + for i := 0; i < len(board); i++ { + for j := 0; j < len(board[0]); j++ { + if board[i][j] == word[0] { + if bfs(board,i,j,isVisited,word,0) { + return true + } + } + } + } + return false +} + +func bfs(board [][]byte, i,j int, isVisited [][]bool, word string, index int) bool { + if index == len(word) { + return true + } + if i < 0 || j < 0 || i == len(board) || j == len(board[0]) || isVisited[i][j] || board[i][j] != word[index] { + return false + } + isVisited[i][j] = true + res := bfs(board, i+1, j, isVisited, word, index+1) || + bfs(board, i, j+1, isVisited, word, index+1) || + bfs(board, i-1, j, isVisited, word, index+1) || + bfs(board, i, j-1, isVisited, word, index+1) + isVisited[i][j] = false + return res +} +``` + + + ### ... + ``` ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.go" "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.go" new file mode 100644 index 0000000000000..97babb4f1e529 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.go" @@ -0,0 +1,36 @@ +func exist(board [][]byte, word string) bool { + if len(board) == 0 { + return false + } + //标记数组 + isVisited := make([][]bool,len(board)) + for i := 0; i < len(board); i++ { + isVisited[i] = make([]bool, len(board[0])) + } + for i := 0; i < len(board); i++ { + for j := 0; j < len(board[0]); j++ { + if board[i][j] == word[0] { + if bfs(board,i,j,isVisited,word,0) { + return true + } + } + } + } + return false +} + +func bfs(board [][]byte, i,j int, isVisited [][]bool, word string, index int) bool { + if index == len(word) { + return true + } + if i < 0 || j < 0 || i == len(board) || j == len(board[0]) || isVisited[i][j] || board[i][j] != word[index] { + return false + } + isVisited[i][j] = true + res := bfs(board, i+1, j, isVisited, word, index+1) || + bfs(board, i, j+1, isVisited, word, index+1) || + bfs(board, i-1, j, isVisited, word, index+1) || + bfs(board, i, j-1, isVisited, word, index+1) + isVisited[i][j] = false + return res +} \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" index b35918721c3c6..3c2d434e4282c 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" @@ -127,7 +127,31 @@ var movingCount = function(m, n, k) { }; ``` +### Go + +```go +func movingCount(m int, n int, k int) int { + var visited [][]bool + visited = make([][]bool,m) + for i:=0; i< m; i++ { + visited[i] = make([]bool,n) + } + return dfs(0,0,m,n,k,visited) +} + +func dfs(x,y,m,n,k int, visited [][]bool) int { + if x >= m || y >=n || visited[x][y] || (x%10+x/10+y%10+y/10) >k { + return 0 + } + visited[x][y] = true + return 1+ dfs(x+1,y,m,n,k,visited)+dfs(x,y+1,m,n,k,visited) +} +``` + + + ### ... + ``` ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.go" "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.go" new file mode 100644 index 0000000000000..e22e3d60d2dc2 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.go" @@ -0,0 +1,16 @@ +func movingCount(m int, n int, k int) int { + var visited [][]bool + visited = make([][]bool,m) + for i:=0; i< m; i++ { + visited[i] = make([]bool,n) + } + return dfs(0,0,m,n,k,visited) +} + +func dfs(x,y,m,n,k int, visited [][]bool) int { + if x >= m || y >=n || visited[x][y] || (x%10+x/10+y%10+y/10) >k { + return 0 + } + visited[x][y] = true + return 1+ dfs(x+1,y,m,n,k,visited)+dfs(x,y+1,m,n,k,visited) +} \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" index a7dff72fa4414..7626f82c8661f 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" @@ -82,7 +82,28 @@ var cuttingRope = function(n) { }; ``` + + +### Go + +```go +func cuttingRope(n int) int { + if n <= 3 { + return n-1 + } + sum := 1 + for n > 4 { + sum *= 3 + n -= 3 + } + return sum*n +} +``` + + + ### ... + ``` ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/Solution.go" "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/Solution.go" new file mode 100644 index 0000000000000..2e77627ab0c7b --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/Solution.go" @@ -0,0 +1,11 @@ +func cuttingRope(n int) int { + if n <= 3 { + return n-1 + } + sum := 1 + for n > 4 { + sum *= 3 + n -= 3 + } + return sum*n +} \ No newline at end of file