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 ...
随机推荐
- 「thunar」给thunar增加搜索文件功能
1.安装catfish sudo apt-get install catfish 2.配置thunar,添加[自定义动作] 打开 Thunar 后,点击 Edit -> Configure cu ...
- 导出excel部分代码
public static function header_file($doc,$file,$title,$type='Excel5'){ if(!empty($doc)){ $objWriter = ...
- some software that is used to speed up your system
1.RAMDISK take some space in ram and use them as the disk. Primo Ramdisk Server Edition 5.6.0 regist ...
- Android 多线程通信 访问网络
package org.rongguang.testthread; import android.app.Activity; import android.os.Bundle; import andr ...
- 查找(AVL平衡二叉树)
[1]为什么需要平衡二叉树? 矛盾是推进事物向前发展的源动力. 那么平衡二叉树是从哪里来?肯定是有矛盾存在的.请看程来师的分析: [2]什么是平衡二叉树? 平衡二叉树的基本认识: [3]平衡二叉树的构 ...
- Objective-C代码的文件扩展名与数据类型
Objective-C数据类型可以分为:基本数据类型.对象类型和id类型. 基本数据类型有:int.float.double和char类型. 对象类型就是类或协议所声明的指针类型,例如:SAutore ...
- android 学习随笔二十一(内容提供者 )
一.内容提供者* 应用的数据库是不允许其他应用访问的* 内容提供者的作用就是让别的应用访问到你的私有数据* 自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查 ...
- uploadify3.2.1加载时,报NetworkError 404 Not Found或NetworkError forbidden错误
我用的uploadify的版本是3.2.1 在打开配置了uploadify的页面的时候,什么操作都没有,仅仅是打开了页面,在火狐里可以看到一行报错信息,我的uploadify页面 在"/项目 ...
- javaWeb 使用jsp开发 if 标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- PBOC APDU命令解析【转】
转自:http://blog.csdn.net/zuokong/article/details/49335257 版权声明:本文为博主原创文章,未经博主允许不得转载. 应用层发出的命令报文和卡片回送到 ...