题解 | #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';
    }

}

全部评论

相关推荐

点赞 评论 收藏
分享
2024-12-09 12:21
门头沟学院 C++
l11hy:今早刚开,已满足
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务