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 ...
随机推荐
- 【crunch bang】 tint2-用来控制桌面的布局
tint2配置: #--------------------------------------------- # TINT2 CONFIG FILE #----------------------- ...
- Report launcher to run SSRS report subscriptions on demand
http://www.mssqltips.com/sqlservertip/3078/report-launcher-to-run-ssrs-report-subscriptions-on-deman ...
- 五、Java基础---------if else、switch总结
在前几篇博客中主要是以笔者遇到的一些典型的题目为例子而展开的讨论,接下来几篇将是以知识点的结构进行讲述.本文主要是讲述if ()else .if() else if().switch() case 的 ...
- js调用后台方法(如果你能容忍执行的后台方法变成一个常量)
最近一直在做一个电话拨号的系统,系统不大,但是做的时间有点长了.其中用到了一个技术:js调用后台方法.解决这个问题花了不少时间,现如今仍然还有些不明白的地方,今天跟大家分享一下.真正明白的同学欢迎指正 ...
- Fury观后感
刚看完,淋雨汽车回来的,电影很精彩.前期略慢热(我还去了躺厕所),军人的黑色幽默,冷酷的军旅生活作为基调.内容我就不ao述了,新兵蛋诺曼的经历是这部电影的为主线(也有人说诺曼是观众的代入点,准确来说他 ...
- Servlet工作原理(转)
Servlet运行在Servlet容器中,由容器负责Servlet实例的查找及创建工作,并按照Servlet规范的规定调用Servlet的一组方法,这些方法也叫生命周期的方法.具体调用过程如下图所示: ...
- Robotium Recorder的初试
一.安装 资料来自官方 Prerequisites: Install the Java JDK. Install the Android SDK. The ADT bundle with Eclips ...
- C#操作CSV存取类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- PBOC APDU命令解析【转】
转自:http://blog.csdn.net/zuokong/article/details/49335257 版权声明:本文为博主原创文章,未经博主允许不得转载. 应用层发出的命令报文和卡片回送到 ...
- 《深度探索C++对象模型》1
C++对象模型: 多重继承模型示意: 第二章:构造函数 语意学 基类和派生类: Bear yogi; ZooAnimal franny=yogi; 在这里,很容易理解合成的copy构造函数将vptr指 ...