Leetcode 1170. 比较字符串最小字母出现频次
1. 问题描述


2. 思路
2.1 思路1
暴力
3. 代码
3.1 思路1代码
func numSmallerByFrequency(queries []string, words []string) []int {
var res []int
counterQ := getCounter(queries)
counterW := getCounter(words)
for i := 0; i < len(queries); i++ {
counter := 0
for j := 0; j < len(words); j++ {
if counterW[j] > counterQ[i] {
counter++
}
}
res = append(res, counter)
}
return res
}
func getCounter(words []string) []int {
var res []int
for i := 0; i < len(words); i++ {
counter := 0
wordByte := []byte(words[i])
letter := getMinLetter(wordByte)
for j := 0; j < len(wordByte); j++ {
if letter == wordByte[j] {
counter++
}
}
res = append(res, counter)
}
return res
}
func getMinLetter(arr []byte) byte {
min := arr[0]
for i := 0; i < len(arr); i++ {
if arr[i] < min {
min = arr[i]
}
}
return min
}
本文解析了LeetCode题目1170,介绍如何通过计数每个字符出现频率来比较查询字符串中较小字母在给定单词列表中的出现次数。展示了两种暴力解法的代码实现,适合初学者理解字符串处理和计数算法的应用。
403

被折叠的 条评论
为什么被折叠?



