邓心一 7 سال پیش
والد
کامیت
176cb7e12c
10فایلهای تغییر یافته به همراه385 افزوده شده و 100 حذف شده
  1. 0 77
      .vscode/c_cpp_properties.json
  2. 0 22
      .vscode/launch.json
  3. 67 1
      medium/79.go
  4. 32 0
      medium/80.go
  5. 62 0
      medium/81.go
  6. 64 0
      medium/82.go
  7. 70 0
      medium/86.go
  8. 39 0
      medium/89.go
  9. 37 0
      medium/90.go
  10. 14 0
      medium/91.go

+ 0 - 77
.vscode/c_cpp_properties.json

@@ -1,77 +0,0 @@
-{
-    "configurations": [
-        {
-            "name": "Mac",
-            "includePath": [
-                "/usr/include",
-                "/usr/local/include",
-                "${workspaceRoot}"
-            ],
-            "defines": [],
-            "intelliSenseMode": "clang-x64",
-            "browse": {
-                "path": [
-                    "/usr/include",
-                    "/usr/local/include",
-                    "${workspaceRoot}"
-                ],
-                "limitSymbolsToIncludedHeaders": true,
-                "databaseFilename": ""
-            },
-            "macFrameworkPath": [
-                "/System/Library/Frameworks",
-                "/Library/Frameworks"
-            ]
-        },
-        {
-            "name": "Linux",
-            "includePath": [
-                "/usr/include",
-                "/usr/local/include",
-                "${workspaceRoot}"
-            ],
-            "defines": [],
-            "intelliSenseMode": "clang-x64",
-            "browse": {
-                "path": [
-                    "/usr/include",
-                    "/usr/local/include",
-                    "${workspaceRoot}"
-                ],
-                "limitSymbolsToIncludedHeaders": true,
-                "databaseFilename": ""
-            }
-        },
-        {
-            "name": "Win32",
-            "includePath": [
-                "C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.11.25503/include/*",
-                "C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.11.25503/atlmfc/include/*",
-                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/um",
-                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/ucrt",
-                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/shared",
-                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/winrt",
-                "${workspaceRoot}"
-            ],
-            "defines": [
-                "_DEBUG",
-                "UNICODE"
-            ],
-            "intelliSenseMode": "msvc-x64",
-            "browse": {
-                "path": [
-                    "C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.11.25503/include/*",
-                    "C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.11.25503/atlmfc/include/*",
-                    "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/um",
-                    "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/ucrt",
-                    "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/shared",
-                    "C:/Program Files (x86)/Windows Kits/10/Include/10.0.15063.0/winrt",
-                    "${workspaceRoot}"
-                ],
-                "limitSymbolsToIncludedHeaders": true,
-                "databaseFilename": ""
-            }
-        }
-    ],
-    "version": 3
-}

+ 0 - 22
.vscode/launch.json

@@ -1,22 +0,0 @@
-{
-    // 使用 IntelliSense 了解相关属性。 
-    // 悬停以查看现有属性的描述。
-    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
-    "version": "0.2.0",
-    "configurations": [
-        
-        {
-            "name": "Launch",
-            "type": "go",
-            "request": "launch",
-            "mode": "debug",
-            "remotePath": "",
-            "port": 2345,
-            "host": "127.0.0.1",
-            "program": "${fileDirname}",
-            "env": {},
-            "args": [],
-            "showLog": true
-        }
-    ]
-}

+ 67 - 1
medium/79.go

@@ -2,8 +2,73 @@ package main
 
 import "fmt"
 
-func exist(board [][]byte, word string) bool {
+// 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
 }
 
@@ -17,4 +82,5 @@ func main() {
 	fmt.Println(exist(b1, "SEE"))
 	fmt.Println(exist(b1, "ABCB"))
 	fmt.Println(exist(b1, "FSADEESCCBA"))
+	fmt.Println(exist(b1, "ZSADEESCCBA"))
 }

+ 32 - 0
medium/80.go

@@ -0,0 +1,32 @@
+package main
+
+import (
+	"fmt"
+)
+
+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{}))
+}

+ 62 - 0
medium/81.go

@@ -0,0 +1,62 @@
+package main
+
+import (
+	"fmt"
+)
+
+// ??? 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] {
+			beg = mid + 1
+		} else {
+			return true
+		}
+	}
+	return false
+}
+
+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))
+}

+ 64 - 0
medium/82.go

@@ -0,0 +1,64 @@
+package main
+
+import (
+	"fmt"
+)
+
+// 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)))
+}

+ 70 - 0
medium/86.go

@@ -0,0 +1,70 @@
+package main
+
+import (
+	"fmt"
+)
+
+// 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)))
+}

+ 39 - 0
medium/89.go

@@ -0,0 +1,39 @@
+package main
+
+import (
+	"fmt"
+)
+
+// 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))
+}

+ 37 - 0
medium/90.go

@@ -0,0 +1,37 @@
+package main
+
+import (
+	"fmt"
+	"sort"
+)
+
+func combine(nums []int, k int) (res [][]int) {
+	n := len(nums)
+	if k == 0 || k == n {
+		row := make([]int, k)
+		for i := range row {
+			row[i] = nums[i]
+		}
+		return append(res, row)
+	}
+	for _, row := range combine(nums[:n-1], k-1) {
+		row = append(row, nums[n-1])
+		res = append(res, row)
+	}
+	res = append(res, combine(nums[:n-1], k)...)
+	return res
+}
+
+func subsetsWithDup(nums []int) [][]int {
+	sort.Ints(nums)
+	res := [][]int{}
+	for i := 0; i <= len(nums); i++ {
+		res = append(res, combine(nums, i)...)
+	}
+	return res
+}
+
+func main() {
+	arr := []int{1, 3, 1}
+	fmt.Println(subsetsWithDup(arr))
+}

+ 14 - 0
medium/91.go

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