leetcode_Repeated DNA Sequences
描写叙述:
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的更多相关文章
- LeetCode-Repeated DNA Sequences (位图算法减少内存)
Repeated DNA Sequences All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, ...
- lc面试准备:Repeated DNA Sequences
1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)
187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [Leetcode] Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 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 ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- LeetCode() Repeated DNA Sequences 看的非常的过瘾!
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- nginx 集群
Nginx是什么? Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器.一直纳闷这个X是怎么来 ...
- python 03 8/25-8/27 range 、randint
import random """字符串的操作中 三种方法,只包含左索引,不包含右索引""" hi= "bokeyuan pyth ...
- Python中的数据类型常见方法
list() 方法 1.cmp(list1, list2):#比较两个列表的元素 2.len(list):#列表元素个数 3.max(list):#返回列表元素最大值 4.min(list):#返回列 ...
- STM32--TIM定时器时钟分割(疑难)
不太明白 (1) TIM_Perscaler来设置预分频系数: (2) TIM_ClockDivision来设置时钟分割(时钟分频因子): (3) TIM_C ...
- Java学习关于集合框架的基础接口--Collection接口
集合框架(Collection Framework)是Java最强大的子系统之一,位于java.util 包中.集合框架是一个复杂的接口与和类层次,提供了管理对象组的最新技术.Java集合框架标准 ...
- .NET Core使用log4Net记录日志
1.引入Nuget包 log4net 2.添加log4Net配置文件 <?xml version="1.0" encoding="utf-8" ?> ...
- 二分图最大权完美匹配KM算法
KM算法二分图 KM求得二分图与普通二分图的不同之处在于:此二分图的每条边(男生女生)上都附了权值(好感度).然后,求怎样完美匹配使得权值之和最大. 这,不止一般的麻烦啊. 可以通过一个期望值来求. ...
- BZOJ1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名
n<=1000头牛各有一个未知值Ai,已知m<=10000条形如Ax>Ay的不等关系,求将整个序列排序的最少比较次数. Aa>Ab,Ab>Ac -------> A ...
- 【linux】ls与ll区别
ll是一个事先被定义好的别名(alias).别名就是赋予一条命令或者一列命令的名称.可以将别名作为缩写的同义词.在我的Ubuntu系统上,~/.bashrc文件中有这么一条语句alias ll='ls ...
- JS中的call()和apply()方法区别
如 果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而怪异的感觉,因为意识上往往不可能的事偏偏就发生了,甚至觉得不可 理喻.如果在学JavaScript这自由而变幻无穷 ...