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 ...
随机推荐
- [sinatra] Just Do It: Learn Sinatra, Part One Darren Jones
1. Install sinatra gem gem install sinatra --no-ri --no-rdoc 2. Basic App #!/usr/bin/ruby require 's ...
- 真实的C++单例模式举例
把构造函数声明为protected的理由很简单,但把构造函数声明为private的原因却很少知道. 从语法上讲,任何函数如果被声明为private,这个函数就不能从外部调用,构造函数也是函数,相反 ...
- fix org.openqa.selenium.NoSuchWindowException when find element on ie11.
Steps:1.I was able to resolve the issue after adding the site URL under trusted sites of IE. The sam ...
- loadrunner具体实例教你如何进行结果分析
1.对于吞吐量,单位时间内吞吐量越大,说明服务器的处理能越好,而请求数仅表示客户端向服务器发出的请求数,与吞吐量一般是成正比关系. 2.一般瓶颈应该就是某个因素在不断增加,某个相关性能指标也会不断增加 ...
- android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)
ActionBar(V7)的添加非常简单,只需要在AndroidManifest.xml中指定Application或Activity的theme是Theme.Holo或其子类就可以了,在Androi ...
- crontab 误区
# For details see man 4 crontabs# Example of job definition:# .---------------- minute (0 - 59)# | . ...
- 精简高效的CSS命名准则/方法
/* ---------------------single CSS----------------------- */ /* display */ .dn{display:none;} .di{di ...
- PHP无限级分类-递归(不推荐)
[http://www.helloweba.com/view-blog-204.html] 在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性.那么PHP是如何实现无限级分类的呢? ...
- Redis 安装 启动 连接 配置 重启
Linux下安装 ]# wget http://download.redis.io/releases/redis-2.8.17.tar.gz ]# .tar.gz ]# cd redis- ]# ma ...
- $q服务的API详解
下面我们通过讲解$q的API让你更多的了解promise异步编程模式.$q是做为angularjs的一个服务而存在的,只是对promise异步编程模式的一个简化实现版,源码中剔除注释实现代码也就二百多 ...