题解 | #DNA序列#

DNA序列

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        test63();
    }
    public static void test63() {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine();
            int b = in.nextInt();
            // 求结果集,连续的子序列
            List<String> result = new ArrayList<>();
            for (int i = 0; i <= a.length() - b; i++) {
                result.add(a.substring(i, i + b));
            }
			// 求CG出现的次数
            int[] num = new int[result.size()];
            for (int i = 0; i < result.size(); i++) {
                char[] ch = result.get(i).toCharArray();
                int n = 0;
                for (int j = 0; j < b; j++) {
                    if (ch[j] == 'C' || ch[j] == 'G') {
                        n++;
                    }
                }
                num[i] = n;
            }
		    // 求最大值
            int max = 0;
            for (int i = 0; i < num.length; i++) {
                max = Math.max(max, num[i]);
            }
		    // 求最大值索引
            int index = 0;
            for (int i = 0; i < num.length; i++) {
                if (num[i] == max) {
                    index = i;
                    break;
                }
            }
            System.out.println(result.get(index));
        }
    }
}

全部评论

相关推荐

丿南烟丶:黑白模板吧,不要这样花哨的。 主要成就太空了,和获奖融在一起,写一两行就行了。 职业技能不要这样排,就传统的掌握精通什么什么然后举例补充的一些重要技术点。 自我介绍说实话也没啥用,可以删了。 把自己的两个项目方案细节补充上去,为什么这样设计,怎么设计,成果是什么按star法则来写 你要引导面试官来问你的技能和项目,你的获奖和自我介绍别人可能看都不看一眼或者不太在乎,重要的是展示你能干活的能力
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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