dengxinyi 6 éve
szülő
commit
73e657aa4e

+ 14 - 0
medium/201.bitwise-and-of-numbers-range.go

@@ -0,0 +1,14 @@
+import (
+	"fmt"
+)
+
+func rangeBitwiseAnd(m int, n int) int {
+	bits := uint(0)
+	for i := n; 0 < i; i, bits = i>>1, bits+1 {
+	}
+	and := 0
+	for mask := 1 << (bits - 1); 1 <= mask && (m&mask) == (n&mask); mask >>= 1 {
+		and |= (m & mask) // Scan from the highest bit, just like 'longest common prefix'
+	}
+	return and
+}

+ 45 - 0
medium/207.course-schedule.go

@@ -0,0 +1,45 @@
+// Will visit, visiting, visited
+const (
+	WILL = iota
+	ING
+	ED
+)
+
+func canFinish(numCourses int, prerequisites [][]int) bool {
+	adj := make([][]bool, numCourses)
+	for i := range adj {
+		adj[i] = make([]bool, numCourses)
+	}
+	for i := range prerequisites {
+		adj[prerequisites[i][0]][prerequisites[i][1]] = true
+	}
+	tag := make([]int, numCourses)
+	hasRing := false
+	for i := 0; i < numCourses; i++ {
+		dfs(i, adj, &tag, &hasRing)
+		if hasRing {
+			return false
+		}
+	}
+	return true
+}
+
+func dfs(x int, adj [][]bool, tag *[]int, hasRing *bool) {
+	if *hasRing || (*tag)[x] == ED {
+		return
+	}
+	(*tag)[x] = ING
+	for i := range adj[x] {
+		if !adj[x][i] {
+			continue
+		}
+		if (*tag)[i] == WILL {
+			dfs(i, adj, tag, hasRing)
+		} else if (*tag)[i] == ING {
+			*hasRing = true
+			return
+		}
+	}
+	(*tag)[x] = ED
+}
+

+ 65 - 0
medium/208.implement-trie-prefix-tree.go

@@ -0,0 +1,65 @@
+type Node struct {
+	IsKey bool
+	Children *[26]Node
+}
+
+
+type Trie struct {
+	Root *Node
+}
+
+
+/** Initialize your data structure here. */
+func Constructor() Trie {
+	return Trie{&Node{}}
+}
+
+
+/** Inserts a word into the trie. */
+func (this *Trie) Insert(word string)  {
+	curr := this.Root
+	var i int
+	for i = range word {
+		if curr.Children == nil {
+			curr.Children = &[26]Node{}
+		}
+		curr = &curr.Children[int(word[i]-'a')]
+	}
+	curr.IsKey = true
+}
+
+
+/** Returns if the word is in the trie. */
+func (this *Trie) Search(word string) bool {
+	curr := this.Root
+	var i int
+	for i = range word {
+		if curr.Children == nil {
+			return false
+		}
+		curr = &curr.Children[int(word[i]-'a')]
+	}
+	return curr.IsKey
+}
+
+
+/** Returns if there is any word in the trie that starts with the given prefix. */
+func (this *Trie) StartsWith(prefix string) bool {
+	curr := this.Root
+	for i := range prefix {
+		if curr.Children == nil {
+			return false
+		}
+		curr = &curr.Children[int(prefix[i]-'a')]
+	}
+	return curr.IsKey || curr.Children != nil
+}
+
+
+/**
+ * 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);
+ */

+ 16 - 0
medium/209.minimum-size-subarray-sum.go

@@ -0,0 +1,16 @@
+func minSubArrayLen(s int, nums []int) int {
+	sum, minLen := 0, len(nums)
+	for slow, fast := 0, 0; fast < len(nums); fast++ {
+		sum += nums[fast]
+		for ; s <= sum-nums[slow] && slow < fast; slow++ {
+			sum -= nums[slow]
+		}
+		if s <= sum && fast-slow < minLen {
+			minLen = fast - slow + 1
+		}
+	}
+	if s <= sum {
+		return minLen
+	}
+	return 0
+}