dengxinyi пре 6 година
родитељ
комит
6e5344156a
4 измењених фајлова са 164 додато и 3 уклоњено
  1. 38 3
      medium/134.go
  2. 40 0
      medium/137.go
  3. 75 0
      medium/138.js
  4. 11 0
      medium/139.go

+ 38 - 3
medium/134.go

@@ -1,9 +1,44 @@
 package main
 package main
 
 
-func canCompleteCircuit(gas []int, cost []int) int {
+// It is toooo slow
+func canCompleteCircuitOld(gas []int, cost []int) int {
+	length := len(gas)
+	for beg := 0; beg < length; beg++ {
+		curr := (beg + 1) % length
+		gasLeft := gas[beg] - cost[beg]
+		for gasLeft >= 0 {
+			if curr == beg {
+				return curr
+			}
+			gasLeft += gas[curr] - cost[curr]
+			curr = (curr + 1) % length
+		}
+	}
 	return -1
 	return -1
 }
 }
 
 
-func main() {
-
+// If car starts at A and can not reach B. Any station between A and B
+// can not reach B. (B is the first station that A can not reach.)
+// If the total number of gas is bigger than the total number of cost.
+// There must be a solution.
+func canCompleteCircuit(gas []int, cost []int) int {
+	var total, sum, beg int
+	for i := 0; i < len(gas); i++ {
+		total += gas[i] - cost[i]
+		sum += gas[i] - cost[i]
+		if sum < 0 {
+			beg = i + 1
+			sum = 0
+		}
+	}
+	if total < 0 {
+		return -1
+	}
+	return beg
 }
 }
+
+// func main() {
+// 	println(canCompleteCircuit(
+// 		[]int{1, 2, 3, 4, 5},
+// 		[]int{3, 4, 5, 1, 2}))
+// }

+ 40 - 0
medium/137.go

@@ -0,0 +1,40 @@
+package main
+
+func singleNumberOld(nums []int) int {
+	set := make(map[int]bool, 0)
+	sumNums := 0
+	for _, v := range nums {
+		sumNums += v
+		if !set[v] {
+			set[v] = true
+		}
+	}
+	sumSet := 0
+	for k := range set {
+		sumSet += k
+	}
+	return (sumSet*3 - sumNums) / 2
+}
+
+// x2 x1   x2 x1   x2 x1   x2 x1   x2 x1
+//  0  0 -> 0  1 -> 1  0 -> 1  1 -> 0  0
+// So: x1 ^= i, x2 ^= (x1 & i), ... , xn ^= (x[n-1] & ... & x1 & i)
+// And we need a mask to set x1~xn to 0 when counter reaches k,
+// which means mask == 0 only when count == k. For example, when k is 3,
+// mask = 11, ie. mask = !(x2 & x1)
+func singleNumber(nums []int) int {
+	x2, x1, mask := 0, 0, 0
+	for _, i := range nums {
+		x2 ^= (x1 & i)
+		x1 ^= i
+		mask = ^(x2 & x1)
+		x2 &= mask
+		x1 &= mask
+	}
+	return x1
+}
+
+// func main() {
+// 	println(singleNumber(
+// 		[]int{0, 1, 0, 1, 99, 1, 0}))
+// }

+ 75 - 0
medium/138.js

@@ -0,0 +1,75 @@
+'use strict'
+
+/**
+ * Definition for singly-linked list with a random pointer.
+ * function RandomListNode(label) {
+ *     this.label = label
+ *     this.next = this.random = null
+ * }
+ */
+
+function RandomListNode(label) {
+    this.label = label
+    this.next = this.random = null
+}
+
+/**
+ * @param {RandomListNode} head
+ * @return {RandomListNode}
+ */
+var copyRandomList = function (head) {
+    if (head === null) return null
+    const newHead = new RandomListNode(head.label)
+    const map = new Map()
+    if (head.random !== null) {
+        if (map.has(head.random))
+            map.get(head.random).push(newHead)
+        else
+            map.set(head.random, [newHead])
+    }
+    let prev = newHead
+    let curr = head.next
+    while (curr !== null) {
+        const node = new RandomListNode(curr.label)
+        prev.next = node
+        if (curr.random !== null) {
+            if (map.has(curr.random))
+                map.get(curr.random).push(node)
+            else
+                map.set(curr.random, [node])
+        }
+        prev = node
+        curr = curr.next
+    }
+    if (map.size != 0) {
+        for (curr = head, prev = newHead; curr !== null; curr = curr.next, prev = prev.next) {
+            if (map.has(curr))
+                for (const n of map.get(curr)) n.random = prev
+        }
+    }
+    return newHead
+}
+
+function __main__() {
+    /* eslint no-console: ["error", {"allow": ["log"]}] */
+    const logger = console.log.bind(console)
+    const n5 = new RandomListNode(5)
+    const n4 = new RandomListNode(4)
+    const n3 = new RandomListNode(3)
+    const n2 = new RandomListNode(2)
+    const n1 = new RandomListNode(1)
+    n1.next = n2
+    n2.next = n3
+    n3.next = n4
+    n4.next = n5
+    n1.random = n4
+    n2.random = n1
+    n3.random = n5
+    n4.random = null
+    n5.random = n3
+    const n = copyRandomList(n1)
+    logger(n)
+    logger(n1)
+}
+
+__main__()

+ 11 - 0
medium/139.go

@@ -0,0 +1,11 @@
+package main
+
+func wordBreak(s string, wordDict []string) bool {
+	return false
+}
+
+func main() {
+	println(wordBreak(
+		"catsanddogsand",
+		[]string{"cats", "and", "sand", "dogs", "dog"}))
+}