dengxinyi 6 年 前
コミット
1bae75d0ca

+ 35 - 0
medium/522.longest-uncommon-subsequence-ii.go

@@ -0,0 +1,35 @@
+type strslice []string
+
+func (ss strslice) Len() int           { return len(ss) }
+func (ss strslice) Less(i, j int) bool { return len(ss[j]) < len(ss[i]) }
+func (ss strslice) Swap(i, j int)      { ss[i], ss[j] = ss[j], ss[i] }
+
+func findLUSlength(strs []string) int {
+	sort.Sort(strslice(strs))
+	m := make(map[string]int)
+	for i := range strs {
+		m[strs[i]]++
+	}
+	for i := range strs {
+		if val, _ := m[strs[i]]; val == 1 {
+			j := 0
+			for ; j < i && !isSubstr(strs[j], strs[i]); j++ {
+			}
+			if j == i {
+				return len(strs[i])
+			}
+		}
+	}
+	return -1
+}
+
+func isSubstr(s, t string) bool {
+	i, j, m, n := 0, 0, len(s), len(t)
+	s1, s2 := []rune(s), []rune(t)
+	for ; i < m && j < n; j++ {
+		for ; i < m && s1[i] != s2[j]; i++ {
+		}
+		i++
+	}
+	return i <= m && j == n
+}

+ 21 - 0
medium/523.continuous-subarray-sum.go

@@ -0,0 +1,21 @@
+func checkSubarraySum(nums []int, k int) bool {
+	n := len(nums)
+	if n < 2 {
+		return false
+	}
+	dp := make([]int, n)
+	copy(dp, nums)
+	for l := n - 1; 1 <= l; l-- {
+		for i := 0; i < l; i++ {
+			dp[i] += nums[i+n-l]
+			if k == 0 {
+				if dp[i] == k {
+					return true
+				}
+			} else if dp[i]%k == 0 {
+				return true
+			}
+		}
+	}
+	return false
+}

+ 34 - 0
medium/524.longest-word-in-dictionary-through-deleting.go

@@ -0,0 +1,34 @@
+type strslice []string
+
+func (ss strslice) Len() int { return len(ss) }
+func (ss strslice) Less(i, j int) bool {
+	if m, n := len(ss[i]), len(ss[j]); m != n {
+		return n < m
+	}
+	return ss[i] < ss[j]
+}
+func (ss strslice) Swap(i, j int) { ss[i], ss[j] = ss[j], ss[i] }
+
+func findLongestWord(s string, d []string) string {
+	sort.Sort(strslice(d))
+	for i := range d {
+		if isSubstr(s, d[i]) {
+			return d[i]
+		}
+	}
+	return ""
+}
+
+func isSubstr(s, t string) bool {
+	i, j, m, n := 0, 0, len(s), len(t)
+	if m < n {
+		return false
+	}
+	s1, s2 := []rune(s), []rune(t)
+	for ; i < m && j < n; j++ {
+		for ; i < m && s1[i] != s2[j]; i++ {
+		}
+		i++
+	}
+	return i <= m && j == n
+}