题解 | #DNA序列#

DNA序列

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

滑动窗口

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String original = scan.nextLine();
        char[] chrs = original.toCharArray();
        int len = Integer.valueOf(scan.nextLine().trim());
        int ans = 0;
        int ml = 0;
        int acc = 0;
        LinkedList<Integer> ll = new LinkedList<>();
        for (int i = 0; i < chrs.length; i++) {
            char currentChr = chrs[i];
            ll.add(i);
            if (currentChr == 'C' || currentChr == 'G') {
                acc++;
            }
            if (!ll.isEmpty() && ll.peekFirst() == i - len) {
                if (chrs[ll.peekFirst()] == 'C' || chrs[ll.peekFirst()] == 'G') {
                    acc--;
                }
                ll.removeFirst();
            }
            if (i >= len - 1) {
                if (acc > ml) {
                    ml = acc;
                    ans = ll.peekFirst();
                }
            }
        }
        System.out.println(original.substring(ans, ans + len));
    }
}
全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-27 11:42

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务