瀏覽代碼

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

dengxinyi 6 年之前
父節點
當前提交
1fd919009d

+ 43 - 0
hard/591.tag-validator.go

@@ -0,0 +1,43 @@
+func isValid(code string) bool {
+	st := make([]string, 0)
+	for i := 0; i < len(code); i++ {
+		if 0 < i && len(st) == 0 { // Unmatched content
+			return false
+		}
+		if strings.HasPrefix(code[i:], "<![CDATA[") { // Parse cdata
+			j := strings.Index(code[i+9:], "]]>")
+			if j < 0 {
+				return false
+			}
+			i += j + 11
+		} else if strings.HasPrefix(code[i:], "</") { // Parse end
+			i += 2
+			j := strings.Index(code[i:], ">")
+			if j < 0 {
+				return false
+			}
+			name := code[i : i+j]
+			if l := len(st); l == 0 || st[l-1] != name {
+				return false
+			} else {
+				st = st[:l-1]
+			}
+			i += j
+		} else if code[i] == '<' { // Parse begin
+			i += 1
+			j := strings.Index(code[i:], ">")
+			if j < 1 || 9 < j {
+				return false
+			}
+			name := code[i : i+j]
+			for _, r := range name {
+				if r < 'A' || 'Z' < r {
+					return false
+				}
+			}
+			st = append(st, name)
+			i += j
+		}
+	}
+	return len(st) == 0
+}

+ 20 - 0
hard/600.non-negative-integers-without-consecutive-ones.go

@@ -0,0 +1,20 @@
+func findIntegers(num int) int {
+	f := make([]int, 31)
+	f[0], f[1] = 1, 2
+	for i := 2; i < 31; i++ {
+		f[i] = f[i-2] + f[i-1]
+	}
+	cnt, pre := 0, 1
+	for k := 30; 0 <= k; k-- { // The highest bit is the sign of int32.
+		if (num>>uint(k))&1 == 1 {
+			cnt += f[k]
+			if pre == 1 {
+				return cnt
+			}
+			pre = 1
+		} else {
+			pre = 0
+		}
+	}
+	return cnt + 1 // Means num itself is also valid.
+}

+ 20 - 0
medium/609.find-duplicate-file-in-system.go

@@ -0,0 +1,20 @@
+func findDuplicate(paths []string) [][]string {
+	res := make([][]string, 0)
+	m := make(map[string]int)
+	idx := 0
+	for _, path := range paths {
+		strs := strings.Split(path, " ")
+		for i := 1; i < len(strs); i++ {
+			names := strings.Split(strs[i], "(")
+			file := strs[0] + "/" + names[0]
+			if v, ok := m[names[1]]; ok {
+				res[v] = append(res[v], file)
+			} else {
+				res = append(res, []string{file})
+				m[names[1]] = idx
+				idx++
+			}
+		}
+	}
+	return res
+}

+ 30 - 0
medium/611.valid-triangle-number.go

@@ -0,0 +1,30 @@
+func triangleNumber(nums []int) int {
+	n, cnt := len(nums), 0
+	sort.Ints(nums)
+	m := make(map[int]int)
+	for i := 0; i < n-2; i++ {
+		for j := i + 1; j < n-1; j++ {
+			sum := nums[i] + nums[j]
+			if v, ok := m[sum]; ok {
+				if j < v {
+					cnt += v - j - 1
+				}
+			} else {
+				beg, end := j, n-1 // Not j+1, but j
+				for beg <= end {
+					mid := beg + (end-beg)/2
+					if nums[mid] < sum {
+						beg = mid + 1 // So beg is the first idx that >= sum
+					} else {
+						end = mid - 1
+					}
+				}
+				m[sum] = beg
+				if j < beg {
+					cnt += beg - j - 1
+				}
+			}
+		}
+	}
+	return cnt
+}

+ 14 - 0
medium/621.task-scheduler.go

@@ -0,0 +1,14 @@
+func leastInterval(tasks []byte, n int) int {
+	freq := make([]int, 26)
+	for _, t := range tasks {
+		freq[t-'A']++
+	}
+	sort.Ints(freq)
+	max, cnt := freq[25], 0
+	for _, i := range freq {
+		if i == max {
+			cnt++
+		}
+	}
+	return (max-1)*n + cnt
+}

+ 2 - 2
medium/81.go

@@ -4,7 +4,7 @@ import (
 	"fmt"
 )
 
-/* func search(nums []int, target int) bool {
+func search(nums []int, target int) bool {
 	beg, end := 0, len(nums)-1
 	// for empty array
 	if end == -1 {
@@ -38,7 +38,7 @@ import (
 		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 {