LeetCode-Repeated DNA
关于位操作符。如<<, value << num ,其中,num指定要位移值value移动的位数,每左移一个位,高阶位都被移出(直接丢掉),并用0填充右边。。
道理明明很简单啊,老娘我高中就会了好不。。可是不知怎么用啊,coding水平太渣,做题太少,从来木有用过。Σ( ° △ °|||)︴
如,在LeetCode中遇到一个Repeat DNA的题目,刚开始很自然的想到用subString()的方法,无奈每个人的DNA序列成千上万,导致时间、空间需求太大,不符合要求。于是,百度之,大神门说要用到Hash映射的方法。涨姿势啊!!
众所周知,在计算机中,符号01串移位的时间总会要比字符串比较的时间复杂度低得多,而在DNA序列中,只会出现A,C,G,T四个符号,因此可以考虑将字符串对应成01串,这样只需“00”,"01","10","11"即可表示以上四个字符,10个字符的比较用一个32位的整数即可表示!!这样比较10位的String变成了比较一个int型整数啊!!神奇
但是,怎么把不同的字符串表示对应成整数呢?一些大神门描述说用一种叫做“字典”的方式,其实在我看来,无非就是Hash加移位麽,具体描述如下:
首先,将四个字符‘A-0,C-1,G-2,T-3’放入一张HashMap中,这样,计算机中的表示即2位的01串。
HashMap<Character, Integer> co = new HashMap<Character, Integer>();
co.put('A', 0);
co.put('B', 1);
co.put('C', 2);
c0.put('D', 3);
第二,那么10位字符串怎么对应成一个32位的整数呢?开始时百思不得其解,其实也很简单,用一个for循环即可。(PS:要自己亲自演示一遍,否则还是云里雾里),这里,首先把前十个对应成整数:
Integer key = 0;
for(int i=0; i<10; i++){
key = (key << 2) + co.get(s.charAt(i));
}//计算完for循环,得到的数值是341,正好对应00^000101010101,即前十个字符AAAAACCCCC!
第三,明白了这个原理,后面的步骤就很简单了。再使用一张HashMap,记录目前只出现一次的连续10字符串,若当前字符串出现在map中,则是repeat,放入最终结果表list中。
第四,还有一个问题,往后移动一个char时,计算方法如同上面的for循环中的计算公式。
好啦啦,大功告,,回家碎觉觉^_^
LeetCode-Repeated DNA的更多相关文章
- [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() Repeated DNA Sequences 看的非常的过瘾!
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- Leetcode: Repeated DNA Sequence
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [LeetCode] Repeated DNA Sequences hash map
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)
187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...
- 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:Repeated DNA Sequences详细题解
题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- 【LeetCode】Repeated DNA Sequences 解题报告
[题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- linux的vim按了ctrl+s之后假死的解决办法
习惯了很多软件的保存的快捷键,经常在vim中按下ctrl+s,然后就发现vim不响应了,之间一直采用kill的方式解决,近来搜了一下,是这样子的: 这时的vim并没有死掉,只是vim不再向终端输出东西 ...
- linux强制umount设备的方法
假如挂载时使用了:mount /dev/sda1 /mnt/sda1 #查找占用设备的pid fuser -m /mnt/sda1 #假如此时得到的pid为12345 kill -9 12345 um ...
- 1行代码,删除svn文件夹
引用:http://www.cnblogs.com/Alexander-Lee/archive/2010/02/23/1671905.html linux操作系统: find -name .svn | ...
- Elasticsearch多索引
在Elasticsearch中,一般的查询都支持多索引.只有文档API或者别名API等不支持多索引操作,因此本篇就翻译一下多索引相关的内容. 首先,先插入几条数据: $ curl -XPOST lo ...
- 从Windows 8 安装光盘安装.NET Framework 3.5.1
在安装一些应用时, 例如安装 Oracle, 可能会缺少了安装 .Net FrameWork 3.5.1 无法继续. 最简单的方法当时是,直接进 控制面板, 在添加删除程序内, 选择增加Windows ...
- gerrit-git
解释为什么gerrit中的push是需要用refs/for/master http://stackoverflow.com/questions/10461214/why-is-git-push-ger ...
- 6*17点阵的Window程序, Java写的。
package com.wulala; import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;imp ...
- 关于Win7 64位 mysql 5.7下载安装问题
1.从官网下载mysql: 网址:http://dev.mysql.com/downloads/mysql/ 这是我们要找的,win7 64位 点击下载: 出现如图所示,我们不必要登录注册,点击红线内 ...
- href="#"会导致location.replace(location.href);脚本不工作
我们经常这样:<a onclick="xxx" href="#" 其实这不是一个好习惯,当点下这个连接后,主页面的URL后面会加个#号,这样就会导致很多J ...
- [转]ConsumeContainerWhitespace property to remove blank space in SSRS 2008 report
转自:http://beyondrelational.com/modules/2/blogs/115/posts/11153/consumecontainerwhitespace-property-t ...