描写叙述:

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.

Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

For example,

Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",

Return:
["AAAAACCCCC", "CCCCCAAAAA"].

思路:

1.非常显然,暴力求解也是一种方法。虽然该方法是不可能的。

2.我们首先来看字母 ”A" "C" “G" "T" 的ASCII码,各自是65, 67, 71, 84,二进制表示为 1000001, 1000011, 1000111, 1010100。能够看到它们的后三位是不同,所以用后三位就能够区分这四个字母。一个字母用3bit来区分,那么10个字母用30bit就够了。用int的第29~0位分表表示这0~9个字符,然后把30bit转化为int作为这个子串的key,放入到HashTable中。以推断该子串是否出现过。

代码:

 public List<String> findRepeatedDnaSequences(String s)
{
List<String>list=new ArrayList<String>();
int strLen=s.length();
if(strLen<=10)
return list;
HashMap<Integer, Integer>map=new HashMap<Integer,Integer>();
int key=0;
for(int i=0;i<strLen;i++)
{
key=((key<<3)|(s.charAt(i)&0x7))&0x3fffffff;//k<<3,key左移3位,也就是将最左边的字符移除
//s.charAt(i)&0x7)获得用于标记s.charAt(i)字符的低3位
//&0x3fffffff抹去key左移三位后多出的高位不相关比特位
if(i<9)continue;
if(map.get(key)==null)//假设没有该整数表示的字符串,将其加入进map中
map.put(key, 1);
else if(map.get(key)==1)//假设存在。说明存在反复字符串并将其加入进结果list中
{
list.add(s.substring(i-9,i+1));
map.put(key, 2);//防止反复加入同样的字符串
}
}
return list;
}

leetcode_Repeated DNA Sequences的更多相关文章

  1. LeetCode-Repeated DNA Sequences (位图算法减少内存)

    Repeated DNA Sequences All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, ...

  2. lc面试准备:Repeated DNA Sequences

    1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  3. LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)

    187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...

  4. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. [Leetcode] Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  6. leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  7. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  8. LeetCode() Repeated DNA Sequences 看的非常的过瘾!

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  9. Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. ajax 请求json数据中json对象的构造获取问题

    前端的界面中,我想通过ajax来调用写好的json数据,并调用add(data)方法进行解析,请求如下: json数据如下: { “type”:"qqq", "lat&q ...

  2. vue 父子组件的加载顺序

    一.加载渲染过程 父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount ...

  3. poj 1664放苹果(转载,不详细,勿点)(递归)

    题目和别人的解析传送门 我的代码 #include<bits/stdc++.h> using namespace std; int f(int m,int n) { ) ; ||m==) ...

  4. Hadoop集群安装指南(CHD5.9.1)(分布式+图文详解)

    centos7.1,CDH5.9.1,3台机器,终极指导安装 下载链接如下: 安装文件下载链接如下: 链接:https://pan.baidu.com/s/1RQYNiWn9a-T8GXcCsoDBs ...

  5. STL源码分析与实现-stl_list容器

    1. stl_list 介绍 今天我们来总结一下stl_List, 通过之前介绍单链表的文章,其实对链表的基本操作已经十分熟悉了,那对于stl_list,无非就是链表结构不一样,至于其中的增删改查的细 ...

  6. 如何用纯 CSS 创作炫酷的同心矩形旋转动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/bMvbRp 可交互视频教 ...

  7. 宝塔apache配置

    apache配置 <VirtualHost *:80> ServerAdmin webmaster@example.com DocumentRoot "/www/wwwroot/ ...

  8. Sublime Text3 解决中文乱码 & 可用注册码 & 设置默认打开方式

    Sublime Text3注册码 Sublime Text Build 3065 License key 复制如下三个任意一个正版注册码即可 -– BEGIN LICENSE -– Andrew We ...

  9. DEV Express中Bar Manager的使用

    未排版 在barManager中可以添加多种元素,如皮肤按钮,复选框等,但是下拉菜单却给出了多个冗余的控件. 遗留问题:怎么设置Bar为大图标,查找是否存在Ribbon控件. Bar 1,       ...

  10. javascript异步处理

    http://www.ruanyifeng.com/blog/2015/04/generator.html