题解 | #重复的DNA序列#

重复的DNA序列

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

分割子串,放入set.如果set中已经存在该子串,那么加入返回列表。再遍历set,找到最先出现的串即可。

public class Solution {
    Set<String> set =new LinkedHashSet<>();
    public String[] repeatedDNA (String DNA) {
        List<String> list = new LinkedList<>();
        while(DNA.length()>=10){
            if(check(DNA.substring(0,10))){
                if(set.contains(DNA.substring(0,10))){
                    if(!list.contains(DNA.substring(0,10)))
                        list.add(DNA.substring(0,10));
                }
                else set.add(DNA.substring(0,10));
            }
            DNA = DNA.substring(1);
        }
        String[] res = new String[list.size()];
        int index = 0;
        for(String s:set){
            if(list.contains(s))res[index++]=s;
        }
        return res;
    }
    public boolean check(String s){
        for(int i=0;i<s.length();++i){
            if(s.charAt(i)=='A'||s.charAt(i)=='C'||s.charAt(i)=='T'||s.charAt(i)=='G')continue;
            else return false;
        }
        return true;
    }
}
全部评论

相关推荐

想熬夜的小飞象在秋招:我觉得这模版挺好啊,可以调大点行距,大佬能不能推荐一下是在哪找的模板
应届生,你找到工作了吗
点赞 评论 收藏
分享
门口唉提是地铁杀:之前b站被一个游戏demo深深的吸引了。看up主页发现是个初创公司,而且还在招人,也是一天60。二面的时候要我做一个登录验证和传输文件两个微服务,做完要我推到github仓库,还要我加上jaeger和一堆运维工具做性能测试并且面试的时候投屏演示。我傻乎乎的做完以后人家跟我说一句现在暂时不招人,1分钱没拿到全是白干
你的秋招第一场笔试是哪家
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 14:08
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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