邓心一 6 gadi atpakaļ
vecāks
revīzija
d53abeb9f0
4 mainītis faili ar 110 papildinājumiem un 20 dzēšanām
  1. 20 20
      hard/23.go
  2. 61 0
      hard/30.go
  3. 20 0
      hard/37.go
  4. 9 0
      hard/helper.go

+ 20 - 20
hard/23.go

@@ -116,23 +116,23 @@ func mergeKLists(lists []*ListNode) *ListNode {
 	return dummy.Next
 }
 
-func main() {
-	l1 := toLinkedList([]int{-9, -7, -7})
-	l2 := toLinkedList([]int{-6, -4})
-	l3 := toLinkedList([]int{-6, -5})
-	l4 := toLinkedList([]int{-9, -8, -6})
-	l5 := toLinkedList([]int{-10})
-	l6 := toLinkedList([]int{-5})
-	lists := []*ListNode{l1, l2, l3, l4, l5, l6}
-	var pq MinPQ
-	pq.Insert(&ListNode{-9, nil})
-	pq.Insert(&ListNode{-6, nil})
-	pq.Insert(&ListNode{-6, nil})
-	pq.Insert(&ListNode{-9, nil})
-	pq.Insert(&ListNode{-10, nil})
-	pq.Insert(&ListNode{-5, nil})
-	for !pq.Empty() {
-		println(pq.DelMin().Val)
-	}
-	printList(mergeKLists(lists))
-}
+// func main() {
+// 	l1 := toLinkedList([]int{-9, -7, -7})
+// 	l2 := toLinkedList([]int{-6, -4})
+// 	l3 := toLinkedList([]int{-6, -5})
+// 	l4 := toLinkedList([]int{-9, -8, -6})
+// 	l5 := toLinkedList([]int{-10})
+// 	l6 := toLinkedList([]int{-5})
+// 	lists := []*ListNode{l1, l2, l3, l4, l5, l6}
+// 	var pq MinPQ
+// 	pq.Insert(&ListNode{-9, nil})
+// 	pq.Insert(&ListNode{-6, nil})
+// 	pq.Insert(&ListNode{-6, nil})
+// 	pq.Insert(&ListNode{-9, nil})
+// 	pq.Insert(&ListNode{-10, nil})
+// 	pq.Insert(&ListNode{-5, nil})
+// 	for !pq.Empty() {
+// 		println(pq.DelMin().Val)
+// 	}
+// 	printList(mergeKLists(lists))
+// }

+ 61 - 0
hard/30.go

@@ -0,0 +1,61 @@
+package main
+
+func findSubstring(s string, words []string) []int {
+	indices := []int{}
+	strLen, wordCnt := len(s), len(words)
+	if strLen == 0 || wordCnt == 0 {
+		return indices
+	}
+	dict := make(map[string]int) // May contains duplicate words!
+	for _, word := range words {
+		dict[word]++
+	}
+	wordLen := len(words[0])
+	for i := 0; i < wordLen; i++ {
+		left, count := i, 0
+		occur := make(map[string]int)
+		for j := i; j <= strLen-wordLen; j += wordLen {
+			word := s[j : j+wordLen]
+			// If word is valid, accumulate results
+			if _, ok := dict[word]; ok {
+				occur[word]++
+				if occur[word] <= dict[word] {
+					count++
+				} else {
+					// One more word occur, shift the left side of window
+					for occur[word] > dict[word] {
+						leftWord := s[left : left+wordLen]
+						occur[leftWord]--
+						if occur[leftWord] < dict[leftWord] {
+							count--
+						}
+						left += wordLen
+					}
+				}
+				// Come to a result
+				if count == wordCnt {
+					indices = append(indices, left)
+					// Advance left side by one word
+					occur[s[left:left+wordLen]]--
+					count--
+					left += wordLen
+				}
+			} else {
+				// Not in word dict, reset all
+				occur = make(map[string]int)
+				count = 0
+				left = j + wordLen
+			}
+		}
+	}
+	return indices
+}
+
+// func main() {
+// 	fmt.Println(findSubstring(
+// 		"wordgoodgoodgoodbestword",
+// 		[]string{"word", "good", "best", "good"}))
+// 	fmt.Println(findSubstring(
+// 		"foobarfoobar",
+// 		[]string{"foo", "bar"}))
+// }

+ 20 - 0
hard/37.go

@@ -0,0 +1,20 @@
+package main
+
+func solveSudoku(board [][]byte) {
+
+}
+
+func main() {
+	b1 := [][]byte{
+		{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
+		{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
+		{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
+		{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
+		{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
+		{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
+		{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
+		{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
+		{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}
+	solveSudoku(b1)
+	printBoard(b1)
+}

+ 9 - 0
hard/helper.go

@@ -89,3 +89,12 @@ func printTree(root *TreeNode) { // Level order traversal
 	}
 	println()
 }
+
+func printBoard(board [][]byte) {
+	for _, row := range board {
+		for _, grid := range row {
+			print(string(grid), " ")
+		}
+		println()
+	}
+}