From ff5fda441c7bce8e1cae631670b7f3e06ff8177c Mon Sep 17 00:00:00 2001
From: xsplus <960346496@qq.com>
Date: Sun, 14 Jul 2019 15:37:34 +0800
Subject: [PATCH 1/2] =?UTF-8?q?Week=5F04=E4=BD=9C=E4=B8=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/algorithm.iml | 8 --
.idea/misc.xml | 6 -
.idea/modules.xml | 8 --
.idea/vcs.xml | 6 -
.idea/workspace.xml | 227 ---------------------------------
Week_04/id_6/LeetCode_053_6.go | 33 +++++
Week_04/id_6/LeetCode_169_6.go | 80 ++++++++++++
Week_04/id_6/LeetCode_208_6.go | 65 ++++++++++
Week_04/id_6/LeetCode_211_6.go | 57 +++++++++
Week_04/id_6/LeetCode_240_6.go | 69 ++++++++++
Week_04/id_6/LeetCode_455_6.go | 25 ++++
Week_04/id_6/LeetCode_714_6.go | 29 +++++
Week_04/id_6/LeetCode_720_6.go | 40 ++++++
13 files changed, 398 insertions(+), 255 deletions(-)
delete mode 100644 .idea/algorithm.iml
delete mode 100644 .idea/misc.xml
delete mode 100644 .idea/modules.xml
delete mode 100644 .idea/vcs.xml
delete mode 100644 .idea/workspace.xml
create mode 100644 Week_04/id_6/LeetCode_053_6.go
create mode 100644 Week_04/id_6/LeetCode_169_6.go
create mode 100644 Week_04/id_6/LeetCode_208_6.go
create mode 100644 Week_04/id_6/LeetCode_211_6.go
create mode 100644 Week_04/id_6/LeetCode_240_6.go
create mode 100644 Week_04/id_6/LeetCode_455_6.go
create mode 100644 Week_04/id_6/LeetCode_714_6.go
create mode 100644 Week_04/id_6/LeetCode_720_6.go
diff --git a/.idea/algorithm.iml b/.idea/algorithm.iml
deleted file mode 100644
index c956989b..00000000
--- a/.idea/algorithm.iml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
deleted file mode 100644
index 28a804d8..00000000
--- a/.idea/misc.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
deleted file mode 100644
index 2ba6d76a..00000000
--- a/.idea/modules.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
deleted file mode 100644
index 94a25f7f..00000000
--- a/.idea/vcs.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
deleted file mode 100644
index e637ba49..00000000
--- a/.idea/workspace.xml
+++ /dev/null
@@ -1,227 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/Week_04/id_6/LeetCode_053_6.go b/Week_04/id_6/LeetCode_053_6.go
new file mode 100644
index 00000000..962b0a71
--- /dev/null
+++ b/Week_04/id_6/LeetCode_053_6.go
@@ -0,0 +1,33 @@
+package main
+
+import "fmt"
+
+/**
+时间复杂度:O(n)
+空间复杂度:O(1)
+ */
+func maxSubArray(nums []int) int {
+ answer := nums[0]
+ sum := 0
+ for _, v := range nums{
+ if sum > 0 {
+ sum += v
+ }else {
+ // 如果 sum 小于 0 那还不如从当前位置开始计算
+ sum = v
+ }
+ answer = maxInt(sum, answer)
+ }
+ return answer
+}
+
+func maxInt(a int, b int) int {
+ if a > b {
+ return a
+ }
+ return b
+}
+
+func main() {
+ fmt.Println("结果是:", maxSubArray([]int{-2,1,-3,4,-1,2,1,-5,4}))
+}
\ No newline at end of file
diff --git a/Week_04/id_6/LeetCode_169_6.go b/Week_04/id_6/LeetCode_169_6.go
new file mode 100644
index 00000000..2ac323cc
--- /dev/null
+++ b/Week_04/id_6/LeetCode_169_6.go
@@ -0,0 +1,80 @@
+package main
+
+import "fmt"
+
+/**
+暴力法
+时间复杂度:O(n^2)
+空间复杂度:O(1)
+ */
+func majorityElement(nums []int) int {
+ majorityCount := len(nums)/2
+ for _, v := range nums {
+ count := 0
+ for _, num := range nums {
+ if v == num {
+ count++
+ }
+ }
+ if count > majorityCount {
+ return v
+ }
+ }
+ return -1
+}
+
+/**
+哈希表
+时间复杂度:O(n)
+空间复杂度:O(n)
+ */
+func majorityElement1(nums []int) int {
+ majorityCount := len(nums)/2
+ m := make(map[int]int, len(nums))
+ for _, num := range nums {
+ if _, ok := m[num]; ok {
+ m[num]++
+ }else {
+ m[num] = 1
+ }
+ }
+ for k := range m {
+ if m[k] > majorityCount {
+ return k
+ }
+ }
+ return -1
+}
+
+/**
+Boyer-Moore 投票算法 (当不存在众数的时候此方法不适合)
+时间复杂度:O(n)
+空间复杂度:O(1)
+*/
+func majorityElement2(nums []int) int {
+ count := 0
+ var candidate int
+
+ for _, num := range nums {
+ if count == 0 {
+ candidate = num
+ }
+ if num == candidate {
+ count += 1
+ }else {
+ count -= 1
+ }
+ }
+ return candidate
+}
+
+func main() {
+ nums := []int{3,2,3}
+ fmt.Println("结果是:", majorityElement(nums))
+
+ nums1 := []int{3,2,3}
+ fmt.Println("结果是:", majorityElement1(nums1))
+
+ nums2 := []int{3,2,3}
+ fmt.Println("结果是:", majorityElement2(nums2))
+}
\ No newline at end of file
diff --git a/Week_04/id_6/LeetCode_208_6.go b/Week_04/id_6/LeetCode_208_6.go
new file mode 100644
index 00000000..822f7d0d
--- /dev/null
+++ b/Week_04/id_6/LeetCode_208_6.go
@@ -0,0 +1,65 @@
+package main
+
+type Trie struct {
+ val byte
+ sons [26]*Trie
+ end int
+}
+
+func Constructor() Trie {
+ return Trie{}
+}
+
+/**
+时间复杂度:O(m) m 为 字符串长度
+空间复杂度:O(m)
+ */
+func (this *Trie) Insert(word string) {
+ node := this
+ size := len(word)
+ for i := 0; i < size; i++ {
+ idx := word[i] - 'a'
+ if node.sons[idx] == nil {
+ node.sons[idx] = &Trie{val:word[i]}
+ }
+ node = node.sons[idx]
+ }
+ node.end++
+}
+
+/**
+时间复杂度:O(m)
+空间复杂度:O(1)
+ */
+func (this *Trie) Search(word string) bool {
+ node := this
+ size := len(word)
+ for i := 0; i < size; i++ {
+ idx := word[i] - 'a'
+ if node.sons[idx] == nil {
+ return false
+ }
+ node = node.sons[idx]
+ }
+ if node.end > 0 {
+ return true
+ }
+ return false
+}
+
+/**
+时间复杂度:O(m)
+空间复杂度:O(1)
+*/
+func (this *Trie) StartsWith(prefix string) bool {
+ node := this
+ size := len(prefix)
+ for i := 0; i < size; i++ {
+ idx := prefix[i] - 'a'
+ if node.sons[idx] == nil {
+ return false
+ }
+ node = node.sons[idx]
+ }
+ return true
+}
\ No newline at end of file
diff --git a/Week_04/id_6/LeetCode_211_6.go b/Week_04/id_6/LeetCode_211_6.go
new file mode 100644
index 00000000..760b7d2d
--- /dev/null
+++ b/Week_04/id_6/LeetCode_211_6.go
@@ -0,0 +1,57 @@
+package main
+
+type WordDictionary struct {
+ sons [26]*WordDictionary
+ end int
+}
+
+
+func Constructor() WordDictionary {
+ return WordDictionary{}
+}
+
+func (this *WordDictionary) AddWord(word string) {
+ for _, c := range word {
+ idx := c - 'a'
+ if this.sons[idx] == nil {
+ this.sons[idx] = &WordDictionary{}
+ }
+ this = this.sons[idx]
+ }
+ this.end++
+}
+
+
+func (this *WordDictionary) Search(word string) bool {
+ for i, c := range word {
+ if c != '.' {
+ idx := c - 'a'
+ if this.sons[idx] == nil {
+ return false
+ }
+ this = this.sons[idx]
+ }else {
+ for _, son := range this.sons {
+ if son == nil {
+ continue
+ }
+ this = son
+ if i == len(word) - 1 {
+ if this.end > 0 {
+ return true
+ }
+ continue
+ }
+ if this.Search(word[(i+1):]) {
+ return true
+ }
+ }
+ return false
+ }
+ }
+
+ if this.end > 0 {
+ return true
+ }
+ return false
+}
\ No newline at end of file
diff --git a/Week_04/id_6/LeetCode_240_6.go b/Week_04/id_6/LeetCode_240_6.go
new file mode 100644
index 00000000..40a7cc4a
--- /dev/null
+++ b/Week_04/id_6/LeetCode_240_6.go
@@ -0,0 +1,69 @@
+package main
+
+import "fmt"
+
+/**
+暴力法
+时间复杂度:O(m*n)
+空间复杂度:O(1)
+ */
+func searchMatrix(matrix [][]int, target int) bool {
+ for _, v := range matrix {
+ for _, vv := range v {
+ if vv == target {
+ return true
+ }
+ }
+ }
+ return false
+}
+
+
+/**
+暴力法 优化版 (利用矩阵 左到右升序 上到下 升序 的特性)
+空间复杂度:O(1)
+*/
+func searchMatrix1(matrix [][]int, target int) bool {
+ m := len(matrix)
+ if m == 0 {
+ return false
+ }
+ n := len(matrix[0])
+ if n == 0 {
+ return false
+ }
+
+ i, j := m-1, 0
+ for i >= 0 && j < n {
+ if matrix[i][j] == target {
+ return true
+ }
+ if matrix[i][j] > target {
+ // 排除 右侧元素,往上找
+ i--
+ }else {
+ // 继续往右找
+ j++
+ }
+ }
+
+ return false
+}
+
+
+func main() {
+ //matrix := [][]int{
+ // {1, 4, 7, 11, 15},
+ // {2, 5, 8, 12, 19},
+ // {3, 6, 9, 16, 22},
+ // {10, 13, 14, 17, 24},
+ // {18, 21, 23, 26, 30},
+ //}
+
+ matrix := [][]int{
+ {5},
+ {6},
+ }
+ fmt.Println("结果是:", searchMatrix(matrix, 5))
+ fmt.Println("结果是:", searchMatrix1(matrix, 5))
+}
\ No newline at end of file
diff --git a/Week_04/id_6/LeetCode_455_6.go b/Week_04/id_6/LeetCode_455_6.go
new file mode 100644
index 00000000..e5517556
--- /dev/null
+++ b/Week_04/id_6/LeetCode_455_6.go
@@ -0,0 +1,25 @@
+package main
+
+import (
+ "fmt"
+ "sort"
+)
+
+func findContentChildren(g []int, s []int) int {
+ sort.Ints(g)
+ sort.Ints(s)
+
+ var i, j, res int
+ for i < len(g) && j < len(s) {
+ if s[j] >= g[i] {
+ i++
+ res++
+ }
+ j++
+ }
+ return res
+}
+
+func main() {
+ fmt.Println("结果是:", findContentChildren([]int{1,2,3}, []int{1,1}))
+}
\ No newline at end of file
diff --git a/Week_04/id_6/LeetCode_714_6.go b/Week_04/id_6/LeetCode_714_6.go
new file mode 100644
index 00000000..c4cd6c2f
--- /dev/null
+++ b/Week_04/id_6/LeetCode_714_6.go
@@ -0,0 +1,29 @@
+package main
+
+import "fmt"
+
+/**
+时间复杂度:O(n)
+空间复杂度:O(1)
+ */
+func maxProfit(prices []int, fee int) int {
+ empty := 0 // 不持有股票时最大利润
+ hold := -prices[0] // 持有股票最大利润
+
+ for i := 1; i < len(prices); i++ {
+ empty = max(empty, hold + prices[i] - fee)
+ hold = max(hold, empty - prices[i])
+ }
+ return empty
+}
+
+func max(a int, b int) int {
+ if a > b {
+ return a
+ }
+ return b
+}
+
+func main() {
+ fmt.Println("结果是:", maxProfit([]int{1, 3, 2, 8, 4, 9}, 2))
+}
\ No newline at end of file
diff --git a/Week_04/id_6/LeetCode_720_6.go b/Week_04/id_6/LeetCode_720_6.go
new file mode 100644
index 00000000..2cabe6db
--- /dev/null
+++ b/Week_04/id_6/LeetCode_720_6.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+ "fmt"
+ "sort"
+)
+
+/**
+时间复杂度:O(n)
+空间复杂度:O(n)
+ */
+func longestWord(words []string) string {
+ if len(words) == 0 {
+ return ""
+ }
+
+ sort.Strings(words)
+ // 初始化 m 存放"合格"的单词
+ m := make(map[string]bool, len(words))
+
+ res := words[0]
+
+ for _, w := range words {
+ n := len(w)
+ if n == 1 {
+ m[w] = true
+ }else if m[w[:(n-1)]] {
+ m[w] = true
+ if n > len(res) {
+ res = w
+ }
+ }
+ }
+ return res
+}
+
+func main() {
+ words := []string{"a", "banana", "app", "appl", "ap", "apply", "apple"}
+ fmt.Println("结果是:", longestWord(words))
+}
From 6288c2f0dedb50bf77828f119eb12d17d6f16d49 Mon Sep 17 00:00:00 2001
From: xsplus <960346496@qq.com>
Date: Sun, 14 Jul 2019 15:40:38 +0800
Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9gitignore?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 1 +
1 file changed, 1 insertion(+)
diff --git a/.gitignore b/.gitignore
index e43b0f98..4befed30 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
.DS_Store
+.idea