题解 | #DNA序列#

DNA序列

https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a

定长的滑动窗口
import java.io.IOException;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        char[] chars = scanner.nextLine().toCharArray();
        int n = scanner.nextInt();
        String maxStr = "";
        int lastMaxCount = 0;
        int curCount = 0;
        for (int start = 0; start <= chars.length - n; start++) {
            int end = start + n - 1;
            if (start == 0) {
                for (int j = start; j <= end; j++) {
                    if (isGC(chars[j])) {
                        curCount++;
                    }
                }
            } else {
                if (isGC(chars[start - 1])) {
                    curCount--;
                }
                if (isGC(chars[end])) {
                    curCount++;
                }
            }
            if (curCount > lastMaxCount) {
                lastMaxCount = curCount;
                maxStr = new String(chars, start, end - start + 1);
            }
        }
        System.out.println(maxStr);
    }
    
    static boolean isGC(char c) {
        return c == 'G' || c == 'C';
    }

}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 17:10
什么素质,我请问呢,要掉小珍珠了。。。又憋屈又生气
苍蓝星上艾露:给它们能的,一群dinner牛马挥刀向更弱者罢了。我写的开源求职AI co-pilot工具,优化你的简历,找到你匹配的岗位,定制你的简历,并让你做好面试准备https://github.com/weicanie/prisma-ai
点赞 评论 收藏
分享
人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-11 11:16
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务