// 判断 s 是否为 t 的子序列 funcisSubseq(s, t string)bool { i := 0 for _, c := range t { if s[i] == byte(c) { i++ if i == len(s) { // 所有字符匹配完毕 returntrue// s 是 t 的子序列 } } } returnfalse } funcfindLUSlength(strs []string)int { ans := -1 sort.Slice(strs, func(i, j int)bool { returnlen(strs[i]) > len(strs[j]) }) next: for i, s := range strs { for j, t := range strs { if j != i && isSubseq(s, t) { continue next } } returnlen(strs[i]) } return ans }
funcmaxIncreasingCells(mat [][]int)int { g := make(map[int][][2]int) for i, row := range mat { for j, x := range row { g[x] = append(g[x], [2]int{i, j}) } } keys := make([]int, 0, len(g)) for k := range g { keys = append(keys, k) } sort.Ints(keys) rowMax := make([]int, len(mat)) colMax := make([]int, len(mat[0])) var ans int for _, k := range keys { pos := g[k] fs := make([]int, len(pos)) for i, p := range pos { fs[i] = max(rowMax[p[0]], colMax[p[1]]) + 1 ans = max(ans, fs[i]) } for i, p := range pos { rowMax[p[0]] = max(rowMax[p[0]], fs[i]) colMax[p[1]] = max(colMax[p[1]], fs[i]) } } return ans }
funccountBeautifulPairs(nums []int)int { ans := 0 cnt := [10]int{} for _, num := range nums { for i := 1; i < 10; i++ { if cnt[i] > 0 && gcd(i, num%10) == 1 { ans += cnt[i] } } for num >= 10 { num /= 10 } cnt[num]++ // 统计最高位的出现次数 } return ans }
functemperatureTrend(temperatureA []int, temperatureB []int)int { sum := 0 ans := 0 for i := 1; i < len(temperatureA); i++ { if isCompare(temperatureA[i], temperatureA[i-1]) == isCompare(temperatureB[i], temperatureB[i-1]) { sum++ ans = max(ans, sum) } else { sum = 0 } } return ans } funcisCompare(a, b int)int { if a > b { return1 } elseif a == b { return0 } return-1 }