Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Week_04/id_29/LeetCode_053_29.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions Week_04/id_29/LeetCode_169_29.go
Original file line number Diff line number Diff line change
@@ -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
}
58 changes: 58 additions & 0 deletions Week_04/id_29/LeetCode_208_29.go
Original file line number Diff line number Diff line change
@@ -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
}
64 changes: 64 additions & 0 deletions Week_04/id_29/LeetCode_211_29.go
Original file line number Diff line number Diff line change
@@ -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
}
81 changes: 81 additions & 0 deletions Week_04/id_29/LeetCode_212_29.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 31 additions & 0 deletions Week_04/id_29/LeetCode_240_29.go
Original file line number Diff line number Diff line change
@@ -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
}
24 changes: 24 additions & 0 deletions Week_04/id_29/LeetCode_720_29.go
Original file line number Diff line number Diff line change
@@ -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
}