diff --git a/Week_04/id_29/LeetCode_053_29.go b/Week_04/id_29/LeetCode_053_29.go new file mode 100644 index 00000000..207ee3f5 --- /dev/null +++ b/Week_04/id_29/LeetCode_053_29.go @@ -0,0 +1,17 @@ +package week4 + +func maxSubArray(nums []int) int { + sum, maxSum := -1<<31, -1<<31 + for _, n := range nums { + sum = max(sum+n, n) + maxSum = max(maxSum, sum) + } + return maxSum +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} diff --git a/Week_04/id_29/LeetCode_169_29.go b/Week_04/id_29/LeetCode_169_29.go new file mode 100644 index 00000000..dda58671 --- /dev/null +++ b/Week_04/id_29/LeetCode_169_29.go @@ -0,0 +1,19 @@ +package week4 + +func majorityElement(nums []int) int { + x, t := nums[0], 1 + + for i := 1; i < len(nums); i++ { + switch { + case x == nums[i]: + t++ + case t > 0: + t-- + default: + x = nums[i] + t = 1 + } + } + + return x +} diff --git a/Week_04/id_29/LeetCode_208_29.go b/Week_04/id_29/LeetCode_208_29.go new file mode 100644 index 00000000..51abdc04 --- /dev/null +++ b/Week_04/id_29/LeetCode_208_29.go @@ -0,0 +1,58 @@ +package week4 + +type Trie struct { + val byte + sons [26]*Trie + end int +} + +func Constructor() Trie { + return Trie{} +} + +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++ +} + +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 +} + +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 +} diff --git a/Week_04/id_29/LeetCode_211_29.go b/Week_04/id_29/LeetCode_211_29.go new file mode 100644 index 00000000..3c48eb26 --- /dev/null +++ b/Week_04/id_29/LeetCode_211_29.go @@ -0,0 +1,64 @@ +package week4 + +type WordDictionary struct { + sons [26]*WordDictionary + end int +} + +/** Initialize your data structure here. */ +func Constructor() WordDictionary { + return WordDictionary{} +} + +/** Adds a word into the data structure. */ +func (this *WordDictionary) AddWord(word string) { + for _, b := range word { + idx := b - 'a' + if this.sons[idx] == nil { + this.sons[idx] = &WordDictionary{} + } + this = this.sons[idx] + } + + this.end++ +} + +/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ +func (this *WordDictionary) Search(word string) bool { + for i, b := range word { + if b != '.' { + idx := b - '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 +} diff --git a/Week_04/id_29/LeetCode_212_29.go b/Week_04/id_29/LeetCode_212_29.go new file mode 100644 index 00000000..1daebdff --- /dev/null +++ b/Week_04/id_29/LeetCode_212_29.go @@ -0,0 +1,81 @@ +package week4 + +func findWords(board [][]byte, words []string) []string { + var results []string + + m := len(board) + if m == 0 { + return results + } + + n := len(board[0]) + if n == 0 { + return results + } + + trie := buildTrie(words) + + for i := 0; i < m; i++ { + for j := 0; j < n; j++ { + dfs(board, i, j, trie, &results) + } + } + + return results +} + +func dfs(board [][]byte, i int, j int, trie *TrieNode, results *[]string) { + c := board[i][j] + if c == '#' || trie.next[int(c-'a')] == nil { + return + } + + trie = trie.next[int(c-'a')] + if len(trie.word) > 0 { + // Found one + *results = append(*results, trie.word) + trie.word = "" + } + + board[i][j] = '#' + if i > 0 { + dfs(board, i-1, j, trie, results) + } + + if i < len(board)-1 { + dfs(board, i+1, j, trie, results) + } + + if j > 0 { + dfs(board, i, j-1, trie, results) + } + + if j < len(board[0])-1 { + dfs(board, i, j+1, trie, results) + } + + board[i][j] = c +} + +func buildTrie(words []string) *TrieNode { + root := new(TrieNode) + for _, word := range words { + cur := root + for _, c := range word { + cidx := int(c - 'a') + if cur.next[cidx] == nil { + cur.next[cidx] = new(TrieNode) + } + cur = cur.next[cidx] + } + cur.word = word + } + + return root +} + +// TrieNode 是 trie 的节点 +type TrieNode struct { + next [26]*TrieNode + word string +} diff --git a/Week_04/id_29/LeetCode_240_29.go b/Week_04/id_29/LeetCode_240_29.go new file mode 100644 index 00000000..2de6952a --- /dev/null +++ b/Week_04/id_29/LeetCode_240_29.go @@ -0,0 +1,31 @@ +package week4 + +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, j := m-1, 0 + for 0 <= i && j < n { + if matrix[i][j] == target { + return true + } + + if matrix[i][j] < target { + j++ + // 排除了 (i,j) 上方的所有比 matrix[i][j] 小的元素 + } else { + i-- + // 排除了 (i,j) 右方的所有比 matrix[i][j] 大的元素 + } + } + // 这种方法的效率,取决于 m 与 n 的相对大小关系 + // 当 m == n 时,效率最高 + // 当 m == 1 或 n ==1 时,效率最低 + return false +} diff --git a/Week_04/id_29/LeetCode_720_29.go b/Week_04/id_29/LeetCode_720_29.go new file mode 100644 index 00000000..49ffb37e --- /dev/null +++ b/Week_04/id_29/LeetCode_720_29.go @@ -0,0 +1,24 @@ +package week4 + +import "sort" + +func longestWord(words []string) string { + sort.Strings(words) + 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 len(res) < len(w) { + res = w + } + } + } + + return res +}