ika 7 år sedan
förälder
incheckning
f1fb19c4bc
9 ändrade filer med 328 tillägg och 0 borttagningar
  1. 2 0
      helper.go
  2. 4 0
      medium/11.go
  3. 67 0
      medium/79.go
  4. 28 0
      medium/80.go
  5. 55 0
      medium/81.go
  6. 60 0
      medium/82.go
  7. 67 0
      medium/86.go
  8. 35 0
      medium/89.go
  9. 10 0
      medium/91.go

+ 2 - 0
helper.go

@@ -4,11 +4,13 @@ import (
 	"fmt"
 )
 
+// ListNode ...
 type ListNode struct {
 	Val  int
 	Next *ListNode
 }
 
+// TreeNode ...
 type TreeNode struct {
 	Val   int
 	Left  *TreeNode

+ 4 - 0
medium/11.go

@@ -47,5 +47,9 @@ func maxArea(height []int) int {
 /* func main() {
 	arr := []int{1, 2, 4, 54, 6, 54}
 	fmt.Println(maxArea(arr))
+<<<<<<< HEAD
 }
 */
+=======
+} */
+>>>>>>> 7f6bbabf3a02854f000cb57e11f470c83197aefb

+ 67 - 0
medium/79.go

@@ -1,5 +1,71 @@
 package main
 
+// Pos stands for the position of board[y][x]
+type Pos struct {
+	x int
+	y int
+}
+
+func existIter(board [][]byte, word string, visited map[Pos]struct{}, last Pos) bool {
+	if _, ok := visited[last]; ok {
+		return false
+	}
+	if len(word) == 0 {
+		return true
+	}
+	visited[last] = struct{}{}
+	for dir := 0; dir < 4; dir++ {
+		var pos Pos
+		switch dir {
+		case 0: // up
+			if last.y > 0 && board[last.y-1][last.x] == word[0] {
+				pos = Pos{last.x, last.y - 1}
+			} else {
+				continue
+			}
+		case 1: // down
+			if last.y < len(board)-1 && board[last.y+1][last.x] == word[0] {
+				pos = Pos{last.x, last.y + 1}
+			} else {
+				continue
+			}
+		case 2: // left
+			if last.x > 0 && board[last.y][last.x-1] == word[0] {
+				pos = Pos{last.x - 1, last.y}
+			} else {
+				continue
+			}
+		default: // right
+			if last.x < len(board[0])-1 && board[last.y][last.x+1] == word[0] {
+				pos = Pos{last.x + 1, last.y}
+			} else {
+				return false
+			}
+		}
+		newMap := make(map[Pos]struct{})
+		for k := range visited {
+			newMap[k] = struct{}{}
+		}
+		if existIter(board, word[1:], newMap, pos) {
+			return true
+		}
+	}
+	return false
+}
+
+// too complex and slow
+func existOld(board [][]byte, word string) bool {
+	beg := word[0]
+	for y, row := range board {
+		for x, v := range row {
+			if beg == v && existIter(board, word[1:], map[Pos]struct{}{}, Pos{x, y}) {
+				return true
+			}
+		}
+	}
+	return false
+}
+
 func exist(board [][]byte, word string) bool {
 	return false
 }
@@ -14,4 +80,5 @@ func exist(board [][]byte, word string) bool {
 	fmt.Println(exist(b1, "SEE"))
 	fmt.Println(exist(b1, "ABCB"))
 	fmt.Println(exist(b1, "FSADEESCCBA"))
+	fmt.Println(exist(b1, "ZSADEESCCBA"))
 } */

+ 28 - 0
medium/80.go

@@ -0,0 +1,28 @@
+package main
+
+func removeDuplicates(nums []int) int {
+	n := len(nums)
+	if n < 3 {
+		return n
+	}
+	res, cnt := 1, 1
+	for i := 1; i < n; i++ {
+		if nums[i] == nums[i-1] {
+			cnt++
+		} else {
+			cnt = 1
+		}
+		if cnt <= 2 {
+			nums[res] = nums[i]
+			res++
+		}
+	}
+	return res
+}
+
+/* func main() {
+	a1 := []int{0, 1, 1, 1, 2, 3, 3, 3}
+	fmt.Println(removeDuplicates(a1))
+	fmt.Println(a1)
+	fmt.Println(removeDuplicates([]int{}))
+} */

+ 55 - 0
medium/81.go

@@ -1,5 +1,6 @@
 package main
 
+<<<<<<< HEAD
 import (
 	"fmt"
 )
@@ -46,12 +47,50 @@ func binarySearch(nums []int, target, beg, end int) bool {
 		if nums[mid] > target {
 			end = mid - 1
 		} else if nums[mid] < target {
+=======
+// ??? so many problems here
+/* func search(nums []int, target int) bool {
+	if len(nums) == 0 {
+		return false
+	}
+	beg, end := 0, len(nums)-1
+	var mid int
+	// how to find the smallest num of arr?
+	for beg < end {
+		mid = (beg + end) / 2
+		if nums[mid] > nums[end] {
+			beg = mid + 1
+		} else if nums[mid] < nums[end] {
+			end = mid - 1
+		} else {
+			for nums[mid] < nums[(mid+1)%len(nums)] {
+				mid = (mid + len(nums) - 1) % len(nums)
+			}
+			break
+		}
+	}
+	fmt.Println(mid)
+	if target == nums[mid] {
+		return true
+	}
+	if target < nums[0] {
+		beg, end = mid+1, len(nums)-1
+	} else {
+		beg, end = 0, mid-1
+	}
+	for beg <= end {
+		mid = (beg + end) / 2
+		if target < nums[mid] {
+			end = mid - 1
+		} else if target > nums[mid] {
+>>>>>>> 7f6bbabf3a02854f000cb57e11f470c83197aefb
 			beg = mid + 1
 		} else {
 			return true
 		}
 	}
 	return false
+<<<<<<< HEAD
 }
 
 func testSearch(nums []int, target int) {
@@ -75,4 +114,20 @@ func testSearch(nums []int, target int) {
 	testSearch(nums2, 0)
 	testSearch(nums3, 5)
 	testSearch(nums4, 0)
+=======
+} */
+
+/* func main() {
+	a1 := []int{4, 5, 6, 6, 6, 7, 7, 0, 0, 1, 1, 2, 2, 3}
+	fmt.Println(search(a1, 6))
+	fmt.Println(search(a1, 4))
+	fmt.Println(search(a1, 3))
+	fmt.Println(search(a1, 7))
+	fmt.Println(search(a1, 9))
+	a2 := []int{1, 3}
+	fmt.Println(search(a2, 3))
+	a3 := []int{3, 1}
+	fmt.Println(search(a3, 3))
+	fmt.Println(search(a3, 1))
+>>>>>>> 7f6bbabf3a02854f000cb57e11f470c83197aefb
 } */

+ 60 - 0
medium/82.go

@@ -0,0 +1,60 @@
+package main
+
+// ListNode ...
+/* type ListNode struct {
+	Val  int
+	Next *ListNode
+} */
+
+/* func list2str(head *ListNode) string {
+	curr := head
+	str := make([]rune, 0)
+	for curr != nil {
+		str = append(str, rune(curr.Val+'0'))
+		curr = curr.Next
+	}
+	return string(str)
+} */
+
+/**
+ * Definition for singly-linked list.
+ * type ListNode struct {
+ *     Val int
+ *     Next *ListNode
+ * }
+ */
+func deleteDuplicates(head *ListNode) *ListNode {
+	if head == nil || head.Next == nil {
+		return head
+	}
+	dummy := ListNode{0, head}
+	slow := &dummy
+	fast := slow.Next
+	for fast != nil {
+		if fast.Next == nil || fast.Next.Val != slow.Next.Val {
+			// duplicates end
+			if slow.Next != fast {
+				// cut dup
+				slow.Next = fast.Next
+				fast = fast.Next
+				// do not move ptr in case of fast maybe nil
+				continue
+			}
+			// no duplicates, move both slow & fasr
+			slow = slow.Next
+		}
+		// if has dup, move fast only
+		fast = fast.Next
+	}
+	return dummy.Next
+}
+
+/* func main() {
+	l15 := ListNode{5, nil}
+	l14 := ListNode{5, &l15}
+	l13 := ListNode{4, &l14}
+	l12 := ListNode{3, &l13}
+	l1 := &ListNode{1, &l12}
+	fmt.Println(list2str(deleteDuplicates(l1)))
+	fmt.Println(list2str(deleteDuplicates(nil)))
+} */

+ 67 - 0
medium/86.go

@@ -0,0 +1,67 @@
+package main
+
+// ListNode ...
+/* type ListNode struct {
+	Val  int
+	Next *ListNode
+} */
+
+/* func list2str(head *ListNode) string {
+	curr := head
+	str := make([]rune, 0)
+	for curr != nil {
+		str = append(str, rune(curr.Val+'0'))
+		curr = curr.Next
+	}
+	return string(str)
+} */
+
+/**
+ * Definition for singly-linked list.
+ * type ListNode struct {
+ *     Val int
+ *     Next *ListNode
+ * }
+ */
+func partition(head *ListNode, x int) *ListNode {
+	if head == nil || head.Next == nil {
+		return head
+	}
+	dummy := ListNode{0, head}
+	fast, slow := &dummy, &dummy
+	// slow -> body -> fast -> target -> tail
+	// => slow -> target -> body -> fast -> tail
+	// => slow -> body -> fast -> tail
+	// or fast/slow -> tail
+	for fast != nil {
+		if fast.Next != nil && fast.Next.Val < x {
+			if fast == slow {
+				fast = fast.Next
+			} else {
+				tail := fast.Next.Next
+				fast.Next.Next = slow.Next
+				slow.Next = fast.Next
+				fast.Next = tail
+			}
+			slow = slow.Next
+		} else {
+			fast = fast.Next
+		}
+	}
+	return dummy.Next
+}
+
+/* func main() {
+	l15 := ListNode{5, nil}
+	l14 := ListNode{0, &l15}
+	l13 := ListNode{6, &l14}
+	l12 := ListNode{3, &l13}
+	l1 := &ListNode{7, &l12}
+	fmt.Println(list2str(partition(l1, 6)))
+	fmt.Println(list2str(partition(nil, 9)))
+	l23 := ListNode{3, nil}
+	l22 := ListNode{2, &l23}
+	l2 := &ListNode{1, &l22}
+	fmt.Println(list2str(partition(l2, 9)))
+}
+*/

+ 35 - 0
medium/89.go

@@ -0,0 +1,35 @@
+package main
+
+// generate gray code directly
+func grayCodeOld(n int) []int {
+	res := make([]int, 1<<uint(n))
+	code := 0
+	for i := 1; i < 1<<uint(n); i++ {
+		if i&1 == 1 {
+			code ^= 1
+		} else {
+			base := 1
+			for ; base&code == 0; base <<= 1 {
+			}
+			code ^= base << 1
+		}
+		res[i] = code
+
+	}
+	return res
+}
+
+func grayCode(n int) (res []int) {
+	m := uint(n)
+	// binary to gray code: G(n) = (B(n)/2) xor B(n)
+	// form wikipedia
+	for i := 0; i < 1<<m; i++ {
+		res = append(res, i)
+		res[i] ^= res[i] >> 1
+	}
+	return
+}
+
+/* func main() {
+	fmt.Println(grayCode(2))
+} */

+ 10 - 0
medium/91.go

@@ -0,0 +1,10 @@
+package main
+
+func numDecodings(s string) int {
+	return 0
+}
+
+/* func main() {
+	str := "12432546"
+	fmt.Println(numDecodings(str))
+} */