ika 7 سال پیش
والد
کامیت
9868f2c5ba
3فایلهای تغییر یافته به همراه150 افزوده شده و 5 حذف شده
  1. 2 5
      medium/11.go
  2. 78 0
      medium/81.go
  3. 70 0
      medium/90.go

+ 2 - 5
medium/11.go

@@ -1,9 +1,5 @@
 package main
 
-import (
-	"fmt"
-)
-
 func minInt(x, y int) int {
 	if x < y {
 		return x
@@ -48,7 +44,8 @@ func maxArea(height []int) int {
 	return max
 }
 
-func main() {
+/* func main() {
 	arr := []int{1, 2, 4, 54, 6, 54}
 	fmt.Println(maxArea(arr))
 }
+*/

+ 78 - 0
medium/81.go

@@ -0,0 +1,78 @@
+package main
+
+import (
+	"fmt"
+)
+
+/* func search(nums []int, target int) bool {
+	beg, end := 0, len(nums)-1
+	// for empty array
+	if end == -1 {
+		return false
+	}
+	// 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[beg] {
+			beg = mid
+		} else if nums[mid] < nums[end] {
+			end = mid
+		} else {
+			// find the top point one by one
+			if nums[beg] > nums[beg+1] {
+				break
+			}
+			beg++
+		}
+	}
+	// 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 nums[mid] > target {
+			end = mid - 1
+		} 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() {
+	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)
+} */

+ 70 - 0
medium/90.go

@@ -0,0 +1,70 @@
+package main
+
+import (
+	"fmt"
+	"sort"
+)
+
+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 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)
+	}
+	return subsetsWithDupIter(nums[1:], append(subsets, newSubsets...), subsetsMap)
+}
+
+func subsetsWithDup(nums []int) [][]int {
+	sort.Ints(nums)
+	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() {
+	nums := []int{1, 2, 1, 2}
+	testSubsetsWithDup(nums)
+}