题解 | #DNA序列#
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
package main
import (
"fmt"
"strings"
)
func main() {
str := ""
n := 0
fmt.Scan(&str)
fmt.Scan(&n)
//记录G 、C 出现最多的 index
maxIndex := 0
//记录G 、C 出现最多的个数
maxCount := 0
//滑动窗口求解出maxIndex
for i := 0; i < len(str)-n; i++ {
tempStr := str[i : i+n]
count := 0
count += strings.Count(tempStr, "C")
count += strings.Count(tempStr, "G")
if count > maxCount {
maxCount = count
maxIndex = i
}
}
//根据 maxIndex输出 子串即可
fmt.Println(str[maxIndex : maxIndex+n])
}



查看14道真题和解析