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 ...
随机推荐
- Java Thread.join()详解
一.使用方式. 二.为什么要用join()方法 三.join方法的作用 join 四.用实例来理解 打印结果: 打印结果: 五.从源码看join()方法 一.使用方式. join是Thread类的 ...
- 【分享】 封装js操作textarea 方法集合(兼容很好)。
请使用下面的btn操作. 虽然你现在看来没什么用,当要用的时候又到处找资料,还不如现在收集一下. 在DOM里面操作textarea里面的字符,是比较麻烦的. 于是我有这个封装分享给大家 ...
- Global Round 2
A - Ilya and a Colorful Walk CodeForces - 1119A Ilya lives in a beautiful city of Chordalsk. There a ...
- thinkphp5 自定义验证码使用
控制器[https://blog.csdn.net/John_rush/article/details/80169702] public function verify(){ $captcha = n ...
- 【thinkPHP5实现文件上传】
上传文件 ThinkPHP5.0对文件上传的支持更加简单. 内置的上传只是上传到本地服务器,上传到远程或者第三方平台的话需要自己扩展. 假设表单代码如下: <form action=" ...
- 交互式数据可视化-D3.js(四)形状生成器
使用JavaScript和D3.js实现数据可视化 形状生成器 线段生成器 var linePath = d3.line() - 使用默认的设置构造一个 line 生成器. linePath.x() ...
- 关于最新版本react-native0.59.x构建的问题解决方案
react-native的版本更新是真的快,几乎几天就是一个小版本,然而在这个过程中,对于新手来说,成功构建一个,并跑起来的项目,还是有一定难度的,各种问题,一不小心,你就会发现你的时间全部都浪费在了 ...
- LeetCode(71) Simplify Path
题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&quo ...
- LeetCode01--两数之和
''' 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给 ...
- cxLookupCombobox的多字段模糊匹配
查了网上很多资料,懒人输入:通过程序使用过滤对话达到自己的目的: 用到cxFilter单元: cbb_DoctorOrder.Properties.View.DataController.Filter ...