邓心一 6 лет назад
Родитель
Сommit
a1c5847a12

+ 16 - 0
hard/233.number-of-digit-one.go

@@ -0,0 +1,16 @@
+func countDigitOne(n int) (ones int) {
+	// Every 1000 has 100 ones at hundreds digit, every 100 has 10 ones at tens digit, ...
+	// Assume the base of curr digit (like 1000) is m, then a = n / m, b = n % m,
+	// If curr digit is 0, the ones of curr dight is a / 10 * base;
+	// curr digit is 1, the ones is a / 10 * base + (b + 1);
+	// curr digit is 2~9, the ones is (a / 10 + 1) * base.
+	// So, the ones of curr digit is (a + 8) / 10 * base + (a % 10 == 1) * (b + 1)
+	for m := 1; m <= n; m *= 10 {
+		a, b := n/m, n%m
+		ones += (a + 8) / 10 * m
+		if a%10 == 1 {
+			ones += b + 1
+		}
+	}
+	return
+}

+ 81 - 0
hard/239.sliding-window-maximum.go

@@ -0,0 +1,81 @@
+import (
+	"container/list"
+)
+
+func maxSlidingWindow(nums []int, k int) (ans []int) {
+	n := len(nums)
+	if n <= 1 || k == 1 {
+		return nums
+	}
+	l := list.New()
+	l.PushBack(0)
+	for i := 1; i < n; i++ {
+		if front := l.Front(); front.Value.(int) <= i-k {
+			l.Remove(front)
+		}
+		for back := l.Back(); back != nil && nums[back.Value.(int)] <= nums[i]; back = l.Back() {
+			l.Remove(back)
+		}
+		l.PushBack(i)
+		if k-1 <= i {
+			ans = append(ans, nums[l.Front().Value.(int)])
+		}
+	}
+	return
+}
+
+func maxSlidingWindowSlow(nums []int, k int) (ans []int) { // If use BST, O(nlog(k))
+	n := len(nums) // Actually, just abit slower.
+	if n <= 1 || k == 1 {
+		return nums
+	}
+	var sl SortedList
+	for i := 0; i < n; i++ {
+		if k <= i {
+			ans = append(ans, sl.Max())
+			sl.Delete(nums[i-k])
+		}
+		sl.Insert(nums[i])
+	}
+	return append(ans, sl.Max())
+}
+
+type SortedList struct {
+	List []int
+	Len  int
+}
+
+func (sl SortedList) Max() int {
+	return sl.List[sl.Len-1]
+}
+
+func (sl SortedList) Search(x int) int {
+	beg, end := 0, sl.Len
+	for beg < end {
+		mid := beg + (end-beg)/2
+		if x < sl.List[mid] {
+			end = mid
+		} else if sl.List[mid] < x {
+			beg = mid+1
+		} else {
+			return mid
+		}
+	}
+	return beg
+}
+
+func (sl *SortedList) Insert(x int) {
+	idx := sl.Search(x)
+	sl.List = append(sl.List, 0)
+	copy(sl.List[idx+1:], sl.List[idx:])
+	sl.List[idx] = x
+	sl.Len++
+}
+
+func (sl *SortedList) Delete(x int) {
+	idx := sl.Search(x)
+	copy(sl.List[idx:], sl.List[idx+1:])
+	sl.Len--
+	sl.List = sl.List[:sl.Len]
+}
+

+ 3 - 0
hard/273.integer-to-english-words.go

@@ -0,0 +1,3 @@
+func numberToWords(num int) string {
+	
+}

+ 15 - 0
hard/282.expression-add-operators.go

@@ -0,0 +1,15 @@
+package main
+
+import (
+	"fmt"
+)
+
+func addOperators(num string, target int) []string {
+	
+}
+
+func dfs(res *[]string, num string, target)
+
+func main() {
+	fmt.Println(addOperators("105", 5))
+}