dengxinyi 6 jaren geleden
bovenliggende
commit
6ee3e22f28

+ 34 - 2
medium/394.decode-string.go

@@ -1,8 +1,40 @@
 func decodeString(s string) string {
 	var sb strings.Builder
+	beg, n := 0, len(s)
+	for i := 0; i < n; {
+		if '0' <= s[i] && s[i] <= '9' {
+			sb.WriteString(s[beg:i])
+			num, lb, rb := findNext(s, i) // Left bracket, right bracket
+			substr := decodeString(s[lb+1 : rb])
+			for j := 0; j < num; j++ {
+				sb.WriteString(substr)
+			}
+			beg, i = rb+1, rb+1
+		} else {
+			i++
+		}
+	}
+	if beg != n { // Write the last part
+		sb.WriteString(s[beg:n])
+	}
 	return sb.String()
 }
 
-func parseNext(s string, beg int) (s string, beg, end int)  {
-	return -1
+func findNext(s string, beg int) (int, int, int) {
+	lb := beg + 1
+	for ; s[lb] != '['; lb++ {
+	} // Find lb
+	num, _ := strconv.Atoi(s[beg:lb]) // Get repeat count
+	cnt, n := 0, len(s)
+	for i := lb; i < n; i++ {
+		if s[i] == '[' {
+			cnt++
+		} else if s[i] == ']' {
+			cnt--
+			if cnt == 0 {
+				return num, lb, i // Find rb
+			}
+		}
+	}
+	return num, lb, lb // Error
 }

+ 38 - 0
medium/395.longest-substring-with-at-least-k-repeating-characters.go

@@ -0,0 +1,38 @@
+func longestSubstring(s string, k int) (max int) {
+	freq, ban := make([]int, 256), make([]bool, 256)
+	for _, r := range s {
+		freq[r]++
+	}
+	cnt := 0
+	for i := 'a'; i <= 'z'; i++ {
+		if 0 < freq[i] && freq[i] < k {
+			cnt++
+			ban[i] = true
+		}
+	}
+	n := len(s)
+	if cnt == 0 { // No rune is forbidden, return n
+		return n
+	}
+	beg := 0
+	for i, r := range s {
+		if ban[r] { // Split s into substrings using forbidden runes, then find the longest
+			if k <= i-beg {
+				max = maxInt(max, longestSubstring(s[beg:i], k))
+			}
+			beg = i + 1
+		}
+	}
+	if k <= n-beg {
+		max = maxInt(max, longestSubstring(s[beg:n], k))
+	}
+	return
+}
+
+func maxInt(x, y int) int {
+	if x < y {
+		return y
+	}
+	return x
+}
+

+ 18 - 0
medium/396.rotate-function.go

@@ -0,0 +1,18 @@
+func maxRotateFunction(A []int) int {
+	n := len(A)
+	if n <= 1 {
+		return 0
+	}
+	sum, fx := 0, 0
+	for i, a := range A {
+		sum, fx = sum+a, fx+i*a
+	}
+	max := fx
+	for i := n - 1; 0 < i; i-- {
+		fx += sum - n*A[i]
+		if max < fx {
+			max = fx
+		}
+	}
+	return max
+}

+ 17 - 0
medium/397.integer-replacement.go

@@ -0,0 +1,17 @@
+func integerReplacement(n int) (cnt int) {
+	for n&1 == 0 {
+		cnt++
+		n >>= 1
+	}
+	if n == 1 {
+		return
+	}
+	return cnt + 1 + minInt(integerReplacement(n-1), integerReplacement(n+1))
+}
+
+func minInt(x, y int) int {
+	if x < y {
+		return x
+	}
+	return y
+}

+ 22 - 0
medium/398.random-pick-index.go

@@ -0,0 +1,22 @@
+type Solution struct {
+	index map[int][]int
+}
+
+func Constructor(nums []int) Solution {
+	sol := Solution{index: make(map[int][]int)}
+	for i, n := range nums {
+		sol.index[n] = append(sol.index[n], i)
+	}
+	return sol
+}
+
+func (this *Solution) Pick(target int) int {
+	arr := this.index[target]
+	return arr[rand.Intn(len(arr))]
+}
+
+/**
+ * Your Solution object will be instantiated and called as such:
+ * obj := Constructor(nums);
+ * param_1 := obj.Pick(target);
+ */

+ 3 - 0
medium/399.evaluate-division.go

@@ -0,0 +1,3 @@
+func calcEquation(equations [][]string, values []float64, queries [][]string) []float64 {
+	
+}