很重要的一道题

题型适合在面试的时候考

位操作和哈希表结合

public List<String> findRepeatedDnaSequences(String s) {
/*
寻找出现过一次以上的十个字母长的子串
最简单的想法是把每个长度为10的子串存到hashtable中,但是这肯定不符合出题人的意思,要考察位操作
看了答案,使用位操作,第一次做bit manipulation的题
由于A\C\G\T的ASCII码,后三位各不相同,所以我们只要考虑字符的后三位就行
用一个int类型来代表遍历序列,每次把一个字符添加到序列末尾(添加方式是左移3位然后|上下一个字符的后三位)
这样每次用一个掩码提取后27位并|后一位字符代表当前子串,记录到hashtable中,这样用一个int数字代替一个子串,
会节省内存
这里不直接提取后30位的原因是,如果提取30位再向左移3位会超出int范围,而且32位计算机会溢出
所以先提取27位再左移再或
*/
int l = s.length();
List<String> res = new ArrayList<>();
if(l<=10)
{
return res;
}
Map<Integer,Integer> map = new HashMap<>();
//位操作序列
int cur = 0;
//掩码1,用来提取后27位
int mask = 0x7ffffff;
//先把前27位添加上,以后就可以循环实现了 for (int i = 0; i < 9; i++) {
//每次左移3位,空出位置用于添加,&7是提取后三位
cur = (cur<<3)|(s.charAt(i)&7);
}
//开始记录和查询
for (int i = 9; i < l; i++) {
cur = ((cur&mask)<<3)|(s.charAt(i)&7);
map.put(cur,map.getOrDefault(cur,0)+1);
//只在第二次出现时添加,第三次,第四次...不添加
//一开始想着全部添加到map中在遍历key来添加,但是发现那时候就没有字符index:i了,如果用key还原子串很麻烦
if (map.get(cur)==2)
res.add(s.substring(i-9,i+1));
}
return res;
}

[leetcode]187. Repeated DNA Sequences寻找DNA中重复出现的子串的更多相关文章

  1. [LeetCode] 187. Repeated DNA Sequences 解题思路

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  2. [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 ...

  3. 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 ...

  4. Java for LeetCode 187 Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. [LeetCode#187]Repeated DNA Sequences

    Problem: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...

  6. leetcode 26 80 删除已排序数组中重复的数据

    80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...

  7. LeetCode-Repeated DNA Sequences (位图算法减少内存)

    Repeated DNA Sequences All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, ...

  8. LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)

    187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...

  9. lc面试准备:Repeated DNA Sequences

    1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

随机推荐

  1. C和指针课后练习题4

    1.下面表达式是否合法?如果合法,他执行什么任务? 3* x * x - 4 * x + 6; 合法;他只是执行了表达式求值,但是他的结果并不存于任何地方. 2.赋值语句的语法? 数据类型 变量名 = ...

  2. dubbo源码学习(一)dubbo容器启动流程简略分析

    最近在学习dubbo,dubbo的使用感觉非常的简单,方便,基于Spring的容器加载配置文件就能直接搭建起dubbo,之前学习中没有养成记笔记的习惯,时间一久就容易忘记,后期的复习又需要话费较长的时 ...

  3. AlanShan数据库课程设计报告

    目    录 1.绪论.... 2 1.1前言... 2 1.2社会背景... 2 1.3超市背景... 3 2.系统可行性研究.... 4 2.1 技术可行性研究... 4 2.2 经济可行性研究. ...

  4. PyQt学习随笔:Model/View中诸如DisplayRole的数据角色及含义

    在PyQt中,模型可以针对不同的组件(或者组件的不同部分,比如存储数据.界面展示数据.按钮的提示等)提供不同的数据.例如,Qt.DisplayRole用于视图的文本显示.通常来说,模型中的数据项包含一 ...

  5. sqlite 数据库与mysql 数据库使用区别记录

    遇到了就记点儿. 1.sqlite 中,设置外键关联,没啥用.只有mysql 中可用.

  6. 常见SQL注入点判断

    sql注入手工检测 SQL注入手工检测 1基本检测 数字型 字符型 搜索型 POST注入 布尔盲注 报错注入 堆叠注入 判断是什么数据库 2绕过技巧 大小写 替换关键字 使用编码 注释和符号 等价函数 ...

  7. XSS挑战赛(4)

    16-20关 第十六关 关键代码为: <?php ini_set("display_errors", 0); $str = strtolower($_GET["ke ...

  8. 团队作业4-Day2

    团队作业4-Day2 项目git地址 1. 站立式会议 2. 项目燃尽图 3. 适当的项目截图(部分) 4. 代码/文档签入记录(部分) 5. 每人每日总结 吴梓华:今日进行了小程序与网页代码编写的区 ...

  9. react路由初探(1)

    import React from 'react'; import logo from './logo.svg'; import './App.css'; class About extends Re ...

  10. 题解-CF1307G Cow and Exercise

    CF1307G Cow and Exercise 给 \(n\) 点 \(m\) 边的带权有向图,边 \(i\) 为 \((u_i,v_i,w_i)\).\(q\) 次询问,每次给 \(x_i\),问 ...