Ver Fonte

Merge branch 'master' of ssh://111.230.181.241:1111/ikachan/leetcode-go

邓心一 há 7 anos atrás
pai
commit
f3a7774caf
3 ficheiros alterados com 109 adições e 56 exclusões
  1. 2 2
      medium/11.go
  2. 55 35
      medium/81.go
  3. 52 19
      medium/90.go

+ 2 - 2
medium/11.go

@@ -1,11 +1,11 @@
 package main
 
-func minInt(x, y int) int {
+/* func minInt(x, y int) int {
 	if x < y {
 		return x
 	}
 	return y
-}
+} */
 
 // 穷举
 func maxAreaOld(height []int) int {

+ 55 - 35
medium/81.go

@@ -1,58 +1,78 @@
 package main
 
-// ??? so many problems here
+import (
+	"fmt"
+)
+
 /* func search(nums []int, target int) bool {
-	if len(nums) == 0 {
+	beg, end := 0, len(nums)-1
+	// for empty array
+	if end == -1 {
 		return false
 	}
-	beg, end := 0, len(nums)-1
-	var mid int
-	// how to find the smallest num of arr?
+	// check if the target is at the beg or at the end of the arr
+	if nums[beg] == target || nums[end] == target {
+		return true
+	}
+	// the array is not rotated
+	if nums[beg] < nums[end] {
+		return binarySearch(nums, target, beg, end)
+	}
+	// else find the top of the clip
 	for beg < end {
-		mid = (beg + end) / 2
-		if nums[mid] > nums[end] {
-			beg = mid + 1
+		mid := (beg + end) / 2
+		if nums[mid] > nums[beg] {
+			beg = mid
 		} else if nums[mid] < nums[end] {
-			end = mid - 1
+			end = mid
 		} else {
-			for nums[mid] < nums[(mid+1)%len(nums)] {
-				mid = (mid + len(nums) - 1) % len(nums)
+			// find the top point one by one
+			if nums[beg] > nums[beg+1] {
+				break
 			}
-			break
+			beg++
 		}
 	}
-	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
+	// check the area where the target is in
+	if target > nums[0] {
+		return binarySearch(nums, target, 0, beg)
 	}
+	return binarySearch(nums, target, beg+1, len(nums)-1)
+} */
+
+func binarySearch(nums []int, target, beg, end int) bool {
 	for beg <= end {
-		mid = (beg + end) / 2
-		if target < nums[mid] {
+		mid := (beg + end) / 2
+		if nums[mid] > target {
 			end = mid - 1
-		} else if target > nums[mid] {
+		} else if nums[mid] < target {
 			beg = mid + 1
 		} else {
 			return true
 		}
 	}
 	return false
-} */
+}
+
+func testSearch(nums []int, target int) {
+	isIn := search(nums, target)
+	checkAgain := false
+	for _, num := range nums {
+		if num == target {
+			checkAgain = true
+			break
+		}
+	}
+	fmt.Println(isIn, checkAgain)
+}
 
 /* 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))
+	nums1 := []int{4, 5, 6, 7, 0, 1, 2}
+	nums2 := []int{2, 5, 6, 0, 0, 1, 2}
+	nums3 := []int{1, 2, 3, 4, 5}
+	nums4 := []int{1, 1}
+	testSearch(nums1, 1)
+	testSearch(nums2, 0)
+	testSearch(nums3, 5)
+	testSearch(nums4, 0)
 } */

+ 52 - 19
medium/90.go

@@ -5,33 +5,66 @@ import (
 	"sort"
 )
 
-func combineWithDup(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]
+func minInt(x, y int) int {
+	if x < y {
+		return x
+	}
+	return y
+}
+
+// Int2DSlice ...
+type Int2DSlice [][]int
+
+func (p Int2DSlice) Len() int { return len(p) }
+func (p Int2DSlice) Less(i, j int) bool {
+	if len(p[i]) == 0 {
+		return true
+	}
+	if len(p[j]) == 0 {
+		return false
+	}
+	bound := minInt(len(p[i]), len(p[j]))
+	fmt.Println(p[i][0], p[j][0], bound)
+	for k := 0; k < bound; k++ {
+		if p[i][k] < p[j][k] {
+			return true
 		}
-		return append(res, row)
 	}
-	for _, row := range combineWithDup(nums[:n-1], k-1) {
-		row = append(row, nums[n-1])
-		res = append(res, row)
+	return len(p[i]) == bound
+}
+func (p Int2DSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+
+func subsetsWithDupIter(nums []int, subsets [][]int, subsetsMap map[string]bool) [][]int {
+	if len(nums) == 0 {
+		return subsets
+	}
+	newSubsets := [][]int{}
+	for _, set := range subsets {
+		newSet := append(set, nums[0])
+		if _, ok := subsetsMap[fmt.Sprint(newSet)]; ok {
+			continue
+		}
+		subsetsMap[fmt.Sprint(newSet)] = true
+		newSubsets = append(newSubsets, newSet)
 	}
-	res = append(res, combineWithDup(nums[:n-1], k)...)
-	return res
+	return subsetsWithDupIter(nums[1:], append(subsets, newSubsets...), subsetsMap)
 }
 
 func subsetsWithDup(nums []int) [][]int {
 	sort.Ints(nums)
-	res := [][]int{}
-	for i := 0; i <= len(nums); i++ {
-		res = append(res, combineWithDup(nums, i)...)
-	}
-	return res
+	subsets := [][]int{[]int{}}
+	subsetsMap := make(map[string]bool, 0)
+	result := subsetsWithDupIter(nums, subsets, subsetsMap)
+	sort.Sort(Int2DSlice(result))
+	return result
+}
+
+func testSubsetsWithDup(nums []int) {
+	subsets := subsetsWithDup(nums)
+	fmt.Println("The subsets of", nums, "is", subsets)
 }
 
 func main() {
-	arr := []int{1, 3, 1, 3}
-	fmt.Println(subsetsWithDup(arr))
+	nums := []int{1, 2, 1, 2}
+	testSubsetsWithDup(nums)
 }