From 3bff587dbae72ce394b7e1e637f1c85e7953fde8 Mon Sep 17 00:00:00 2001 From: renzhen Date: Wed, 26 Jun 2019 23:36:47 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E5=91=A8=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E9=A2=98=E7=9B=AE=20Go=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_10/Leecode-169-10.go | 62 +++++++++++++++ Week_04/id_10/Leecode-208-10.go | 62 +++++++++++++++ Week_04/id_10/Leecode-240-10.go | 136 ++++++++++++++++++++++++++++++++ Week_04/id_10/Leecode-455-10.go | 20 +++++ Week_04/id_10/Leecode-62-10.go | 22 ++++++ Week_04/id_10/Leecode-70-10.go | 24 ++++++ Week_04/id_10/Leecode-714-10.go | 49 ++++++++++++ Week_04/id_10/Leecode-78-10.go | 22 ++++++ 8 files changed, 397 insertions(+) create mode 100644 Week_04/id_10/Leecode-169-10.go create mode 100644 Week_04/id_10/Leecode-208-10.go create mode 100644 Week_04/id_10/Leecode-240-10.go create mode 100644 Week_04/id_10/Leecode-455-10.go create mode 100644 Week_04/id_10/Leecode-62-10.go create mode 100644 Week_04/id_10/Leecode-70-10.go create mode 100644 Week_04/id_10/Leecode-714-10.go create mode 100644 Week_04/id_10/Leecode-78-10.go diff --git a/Week_04/id_10/Leecode-169-10.go b/Week_04/id_10/Leecode-169-10.go new file mode 100644 index 00000000..bd4aa71b --- /dev/null +++ b/Week_04/id_10/Leecode-169-10.go @@ -0,0 +1,62 @@ +package leecode + +func majorityElement(nums []int) int { + //分治法 + return helper(nums, 0, len(nums)-1) +} + +func helper(nums []int, low, high int) int { + if low == high { + return nums[low] + } + mid := (low + high) / 2 + left := helper(nums, low, mid) + right := helper(nums, mid+1, high) + if left == right { + return left + } + leftCount := 0 + rightCount := 0 + for i := low; i <= high; i++ { + if nums[i] == left { + leftCount++ + } else if nums[i] == right { + rightCount++ + } + } + if leftCount >= rightCount { + return left + } + return right +} + +func majorityElement3(nums []int) int { + // 题解 Boyer-Moore 投票算法 + count := 1 + num := nums[0] + for i := 1; i < len(nums); i++ { + if count == 0 { + num = nums[i] + count = 1 + } else if num == nums[i] { + count++ + } else { + count-- + } + } + return num +} + +func majorityElement2(nums []int) int { + //使用 map 记录个数 + numMap := make(map[int]int) + numLen := len(nums) + for i := 0; i < numLen; i++ { + num := nums[i] + numMap[num]++ + if numMap[num] > numLen/2 { + return num + } + } + return -1 +} diff --git a/Week_04/id_10/Leecode-208-10.go b/Week_04/id_10/Leecode-208-10.go new file mode 100644 index 00000000..b383e3f9 --- /dev/null +++ b/Week_04/id_10/Leecode-208-10.go @@ -0,0 +1,62 @@ +package leecode + +type Trie struct { + words []*Trie + val byte + is_end bool +} + +/** Initialize your data structure here. */ +func Constructor() Trie { + root := Trie{words: make([]*Trie, 26), is_end: false} + return root +} + +/** Inserts a word into the trie. */ +func (this *Trie) Insert(word string) { + root := this + for i := 0; i < len(word); i++ { + c := word[i] - 'a' + if root.words[c] == nil { + root.words[c] = &Trie{words: make([]*Trie, 26), val: word[i], is_end: false} + } + root = root.words[c] + } + root.is_end = true +} + +/** Returns if the word is in the trie. */ +func (this *Trie) Search(word string) bool { + root := this + for i := 0; i < len(word); i++ { + c := word[i] - 'a' + if root.words[c] != nil { + root = root.words[c] + } else { + return false + } + } + return root.is_end +} + +/** Returns if there is any word in the trie that starts with the given prefix. */ +func (this *Trie) StartsWith(prefix string) bool { + root := this + for i := 0; i < len(prefix); i++ { + c := prefix[i] - 'a' + if root.words[c] != nil { + root = root.words[c] + } else { + return false + } + } + return true +} + +/** + * Your Trie object will be instantiated and called as such: + * obj := Constructor(); + * obj.Insert(word); + * param_2 := obj.Search(word); + * param_3 := obj.StartsWith(prefix); + */ diff --git a/Week_04/id_10/Leecode-240-10.go b/Week_04/id_10/Leecode-240-10.go new file mode 100644 index 00000000..cfdeaa13 --- /dev/null +++ b/Week_04/id_10/Leecode-240-10.go @@ -0,0 +1,136 @@ +package leecode + +func searchMatrix(matrix [][]int, target int) bool { + //题解 + m := len(matrix) + if m == 0 { + return false + } + n := len(matrix[0]) + if n == 0 { + return false + } + i := 0 + j := n - 1 + for i < m && j >= 0 { + if matrix[i][j] == target { + return true + } else if matrix[i][j] < target { + i++ + } else { + j-- + } + } + return false +} + +func searchMatrix2(matrix [][]int, target int) bool { + //分治法 + m := len(matrix) + if m == 0 { + return false + } + n := len(matrix[0]) + if n == 0 { + return false + } + pos1 := []int{0, 0} + pos2 := []int{m - 1, n - 1} + return helper(matrix, pos1, pos2, target) +} + +func binary_search_hen(matrix [][]int, target int, x, y1, y2 int) bool { + // 横向二分查找 + if x >= len(matrix) { + return false + } + l := y1 + r := y2 + for l <= r { + mid := (l + r) / 2 + if matrix[x][mid] == target { + return true + } else if matrix[x][mid] < target { + l = mid + 1 + } else { + r = mid - 1 + } + } + return false +} + +func binary_search_shu(matrix [][]int, target int, y, x1, x2 int) bool { + // 竖向二分查找 + if y >= len(matrix[0]) { + return false + } + l := x1 + r := x2 + for l <= r { + mid := (l + r) / 2 + if matrix[mid][y] == target { + return true + } else if matrix[mid][y] < target { + l = mid + 1 + } else { + r = mid - 1 + } + } + return false +} + +func helper(matrix [][]int, pos1, pos2 []int, target int) bool { + if pos1[0] < 0 || pos1[1] < 0 || pos2[0] < pos1[0] || pos2[1] < pos1[1] { + return false + } + if pos1[0] == pos2[0] && pos1[1] == pos2[1] { + return matrix[pos1[0]][pos1[1]] == target + } + if pos1[0] == pos2[0] { + return binary_search_hen(matrix, target, pos1[0], pos1[1], pos2[1]) + } + if pos1[1] == pos2[1] { + return binary_search_shu(matrix, target, pos1[1], pos1[0], pos2[0]) + } + midx := (pos1[0] + pos2[0]) / 2 + midy := (pos1[1] + pos2[1]) / 2 + if matrix[midx][midy] == target { + return true + } + if matrix[midx][midy] < target { + ret := binary_search_hen(matrix, target, midx, midy+1, pos2[1]) + if ret { + return true + } + ret = binary_search_shu(matrix, target, midy, midx+1, pos2[0]) + if ret { + return true + } + ret = helper(matrix, []int{midx + 1, midy + 1}, pos2, target) + if ret { + return true + } + } else { + ret := binary_search_hen(matrix, target, midx, pos1[1], midy-1) + if ret { + return true + } + ret = binary_search_shu(matrix, target, midy, pos1[0], midx-1) + if ret { + return true + } + ret = helper(matrix, pos1, []int{midx - 1, midy - 1}, target) + if ret { + return true + } + } + ret := helper(matrix, []int{midx + 1, pos1[1]}, []int{pos2[0], midy - 1}, target) + if ret { + return true + } + ret = helper(matrix, []int{pos1[0], midy + 1}, []int{midx - 1, pos2[1]}, target) + if ret { + return true + } + return false +} diff --git a/Week_04/id_10/Leecode-455-10.go b/Week_04/id_10/Leecode-455-10.go new file mode 100644 index 00000000..5d1c09dd --- /dev/null +++ b/Week_04/id_10/Leecode-455-10.go @@ -0,0 +1,20 @@ +package leecode + +import "sort" + +func findContentChildren(g []int, s []int) int { + //贪心算法 饼干最大 配胃口最大的孩子 + sort.Ints(g) + sort.Ints(s) + gi := len(g) - 1 + si := len(s) - 1 + count := 0 + for gi >= 0 && si >= 0 { + if g[gi] <= s[si] { + si-- + count++ + } + gi-- + } + return count +} diff --git a/Week_04/id_10/Leecode-62-10.go b/Week_04/id_10/Leecode-62-10.go new file mode 100644 index 00000000..1b14b86c --- /dev/null +++ b/Week_04/id_10/Leecode-62-10.go @@ -0,0 +1,22 @@ +package leecode + +func uniquePaths(m int, n int) int { + if m == 0 || n == 0 { + return 0 + } + dp := make([][]int, n) + for i := 0; i < n; i++ { + dp[i] = make([]int, m) + dp[i][0] = 1 + } + for i := 0; i < m; i++ { + dp[0][i] = 1 + } + for i := 1; i < n; i++ { + for j := 1; j < m; j++ { + dp[i][j] = dp[i-1][j] + dp[i][j-1] + } + } + return dp[n-1][m-1] + +} diff --git a/Week_04/id_10/Leecode-70-10.go b/Week_04/id_10/Leecode-70-10.go new file mode 100644 index 00000000..c4f5bae2 --- /dev/null +++ b/Week_04/id_10/Leecode-70-10.go @@ -0,0 +1,24 @@ +package leecode + +func climbStairs2(n int) int { + // 递归方法 + if n == 1 || n == 2 { + return n + } + + return climbStairs(n-1) + climbStairs(n-2) +} + +func climbStairs(n int) int { + // dp方法 + if n == 1 || n == 2 { + return n + } + dp := make([]int, n) + dp[0] = 1 + dp[1] = 2 + for i := 2; i < n; i++ { + dp[i] = dp[i-1] + dp[i-2] + } + return dp[n-1] +} diff --git a/Week_04/id_10/Leecode-714-10.go b/Week_04/id_10/Leecode-714-10.go new file mode 100644 index 00000000..a37dee28 --- /dev/null +++ b/Week_04/id_10/Leecode-714-10.go @@ -0,0 +1,49 @@ +package leecode + +func maxProfit(prices []int, fee int) int { + if len(prices) == 0 { + return 0 + } + //dp解法 + dp := make([][]int, len(prices)) + for i := 0; i < len(dp); i++ { + dp[i] = make([]int, 2) + } + dp[0][0] = 0 // 第N天 手里没有股票 赚得最多的钱 + dp[0][1] = 0 - prices[0] // 第N天 手里有股票 赚得最多的钱 + for i := 1; i < len(prices); i++ { + dp[i][0] = max(dp[i-1][1]+prices[i]-fee, dp[i-1][0]) + dp[i][1] = max(dp[i-1][1], dp[i-1][0]-prices[i]) + } + return dp[len(prices)-1][0] +} + +func max(a, b int) int { + if a >= b { + return a + } + return b +} + +func maxProfit2(prices []int, fee int) int { + //贪心算法 + result := 0 + buytime := 0 + selltime := 0 + for i := 1; i < len(prices); i++ { + if selltime > buytime && prices[selltime]-prices[buytime] > fee && prices[selltime]-prices[i] > fee { + result += (prices[selltime] - prices[buytime]) - fee + buytime = i + selltime = i + } else if prices[buytime] > prices[i] { + buytime = i + selltime = i + } else if prices[selltime] < prices[i] { + selltime = i + } + } + if selltime > buytime && prices[selltime]-prices[buytime] > fee { + result += (prices[selltime] - prices[buytime]) - fee + } + return result +} diff --git a/Week_04/id_10/Leecode-78-10.go b/Week_04/id_10/Leecode-78-10.go new file mode 100644 index 00000000..47782f47 --- /dev/null +++ b/Week_04/id_10/Leecode-78-10.go @@ -0,0 +1,22 @@ +package leecode + +func subsets(nums []int) [][]int { + results := make([][]int, 0) + var helper func(nums []int, start int, result []int) + helper = func(nums []int, start int, result []int) { + if start >= len(nums) { + copyresult := make([]int, len(result)) + copy(copyresult, result) + results = append(results, copyresult) + return + } + // 每个数字两种选择 取 不取 + helper(nums, start+1, result) + result = append(result, nums[start]) + helper(nums, start+1, result) + result = result[0 : len(result)-1] + } + result := make([]int, 0) + helper(nums, 0, result) + return results +} From b7de970b556ca97af59e5253ef849cc8ffef07f6 Mon Sep 17 00:00:00 2001 From: renzhen Date: Fri, 28 Jun 2019 22:41:06 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=B0=E5=A2=9Eleecode=2051?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_10/Leecode-51-10.go | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Week_04/id_10/Leecode-51-10.go diff --git a/Week_04/id_10/Leecode-51-10.go b/Week_04/id_10/Leecode-51-10.go new file mode 100644 index 00000000..190ef3dc --- /dev/null +++ b/Week_04/id_10/Leecode-51-10.go @@ -0,0 +1,37 @@ +package leecode + +import "bytes" + +func solveNQueens(n int) [][]string { + // 回溯法 + result := make([][]string, 0) + pie := make([]bool, n*2) + na := make([]bool, n*2) + vlines := make([]bool, n) + qpos := make([]string, n) + var helper func(level int, qpos []string) + helper = func(level int, qpos []string) { + if level == n { + copystrs := make([]string, len(qpos)) + copy(copystrs, qpos) + result = append(result, copystrs) + return + } + for i := 0; i < n; i++ { + if !vlines[i] && !na[i-level+n] && !pie[i+level] { + vlines[i] = true + na[i-level+n] = true + pie[i+level] = true + s := bytes.Repeat([]byte("."), n) + s[i] = 'Q' + qpos[level] = string(s) + helper(level+1, qpos) + vlines[i] = false + na[i-level+n] = false + pie[i+level] = false + } + } + } + helper(0, qpos) + return result +} From e8c924aa0c5bec418a3acfc76d3120ca7068b587 Mon Sep 17 00:00:00 2001 From: renzhen Date: Sat, 29 Jun 2019 23:59:53 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=96=B0=E5=A2=9Eleecode=20123=E5=92=8C?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E6=80=BB=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_10/Leecode-123-10.go | 36 +++++++++++++++++++++++++++++++++ Week_04/id_10/NOTE.md | 7 +++++++ 2 files changed, 43 insertions(+) create mode 100644 Week_04/id_10/Leecode-123-10.go diff --git a/Week_04/id_10/Leecode-123-10.go b/Week_04/id_10/Leecode-123-10.go new file mode 100644 index 00000000..5978b078 --- /dev/null +++ b/Week_04/id_10/Leecode-123-10.go @@ -0,0 +1,36 @@ +package leecode + +func maxProfit(prices []int) int { + if len(prices) == 0 { + return 0 + } + // dp[i][k][t] i 代表第几天 k 代表买入股票k次 t 是否持有股票 0 否 1 是 + dp := make([][][]int, len(prices)) + n := len(prices) + for i := 0; i < len(dp); i++ { + dp[i] = [][]int{[]int{0, 0}, []int{0, 0}, []int{0, 0}} + } + const INT_MIN = 0 - (1e9 + 7) + dp[0][1][1] = 0 - prices[0] + dp[0][1][0] = INT_MIN + dp[0][2][0] = INT_MIN + dp[0][2][1] = INT_MIN + for i := 1; i < len(prices); i++ { + dp[i][1][0] = max(dp[i-1][1][1]+prices[i], dp[i-1][1][0]) + dp[i][1][1] = max(0-prices[i], dp[i-1][1][1]) + dp[i][2][0] = max(dp[i-1][2][1]+prices[i], dp[i-1][2][0]) + dp[i][2][1] = max(dp[i-1][1][0]-prices[i], dp[i-1][2][1]) + } + return max(0, dp[n-1][1][0], dp[n-1][2][0]) + +} + +func max(arr ...int) int { + maxNum := arr[0] + for i := 1; i < len(arr); i++ { + if maxNum < arr[i] { + maxNum = arr[i] + } + } + return maxNum +} diff --git a/Week_04/id_10/NOTE.md b/Week_04/id_10/NOTE.md index 107ea7d6..4c208467 100644 --- a/Week_04/id_10/NOTE.md +++ b/Week_04/id_10/NOTE.md @@ -1 +1,8 @@ # 学习笔记 +这次作业是算法课最后一次提交作业了,这一个月的训练很艰苦,但是对我的帮助很大,我本身工作压力也不小,要在工作之余,抽时间学习,不容易,坚持学习不松懈就更不容易了。越到后面,我越发现自己的热情在消退,一开始做算法题会仔细的分析,会做很详细的总结,做这些并不觉得辛苦。到了最后几周,多做一道题,多写几行字都变得很艰难,心中总有个声音:你的数量足够了,你写得够多了,可以完成作业了。 + +但是每次看到微信群中优秀同学之间的讨论、听到覃超老师直播中精彩的课程、每周review比我好得多的代码和学习总结。就觉得应该再努力努力,于是我又多做了几个题目,总结又多写了几行字。 + +现在回顾自己一个月里自己做的作业和总结,自己也感到骄傲,可能我的代码写得并不优秀,数量也不是那么多,我的总结也没什么技术含量,但是我尝试了,我努力了,我坚持了下来。同时我也看到了自己的不足,现在回头重新做之前的困难题,并不如想象的那么顺手,五毒神掌,我只练了两掌,刻意练习做起来真的很难。所以我还要继续努力。 + +一个月的时间很短暂,一眨眼就结束了,从这门课程中我不止学到了算法的知识,更学到了很多很有价值的方法和思想,很多是我需要继续努力精进的,课程的结束不是终点,而是一个新的起点,未来还需坚持下去,做更好的自己。 \ No newline at end of file