题目

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.

Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

原题链接:https://oj.leetcode.com/problems/repeated-dna-sequences/

straight-forward method(TLE)

算法分析

直接字符串匹配;设计next数组,存字符串中每个字母在其中后续出现的位置;遍历时以next数组为起始。

简化考虑长度为4的字符串

case1:

src A C G T A C G T

next [4] [5] [6] [7] [-1] [-1] [-1] [-1]

那么匹配ACGT字符串的过程,匹配next[0]之后的3位字符即可

case2:

src A C G T A A C G T

next [4] [5] [6] [7] [5] [-1] [-1] [-1] [-1]

多个A字符后继,那么需要匹配所有后继,匹配next[0]不符合之后,还要匹配next[next[0]]

case3:

src A A A A A A

next [1] [2] [3] [4] [5] [-1]

重复的情况,在next[0]匹配成功时,可以把next[next[0]]置为-1,即以next[0]开始的长度为4的字符串已经成功匹配过了,无需再次匹配了;当然这么做只能减少重复的情况,并不能消除重复,因此仍需要使用一个set存储匹配成功的结果,方便去重

时间复杂度

构造next数组的复杂度O(n^2),遍历的复杂度O(n^2);总时间复杂度O(n^2)

代码实现

 #include <string>
#include <vector>
#include <set> class Solution {
public:
std::vector<std::string> findRepeatedDnaSequences(std::string s); ~Solution(); private:
std::size_t* next;
}; std::vector<std::string> Solution::findRepeatedDnaSequences(std::string s) {
std::vector<std::string> rel; if (s.length() <= ) {
return rel;
} next = new std::size_t[s.length()]; // cal next array
for (int pos = ; pos < s.length(); ++pos) {
next[pos] = s.find_first_of(s[pos], pos + );
} std::set<std::string> tmpRel; for (int pos = ; pos < s.length(); ++pos) {
std::size_t nextPos = next[pos];
while (nextPos != std::string::npos) {
int ic = pos;
int in = nextPos;
int count = ;
while (in != s.length() && count < && s[++ic] == s[++in]) {
++count;
}
if (count == ) {
tmpRel.insert(s.substr(pos, ));
next[nextPos] = std::string::npos;
}
nextPos = next[nextPos];
}
} for (auto itr = tmpRel.begin(); itr != tmpRel.end(); ++itr) {
rel.push_back(*itr);
} return rel;
} Solution::~Solution() {
delete [] next;
}

hash table plus bit manipulation method

(view the Show Tags and Runtime 10ms !)

算法分析

首先考虑将ACGT进行二进制编码

A -> 00

C -> 01

G -> 10

T -> 11

在编码的情况下,每10位字符串的组合即为一个数字,且10位的字符串有20位;一般来说int有4个字节,32位,即可以用于对应一个10位的字符串。例如

ACGTACGTAC -> 00011011000110110001

AAAAAAAAAA -> 00000000000000000000

20位的二进制数,至多有2^20种组合,因此hash table的大小为2^20,即1024 * 1024,将hash table设计为bool hashTable[1024 * 1024];

遍历字符串的设计

每次向右移动1位字符,相当于字符串对应的int值左移2位,再将其最低2位置为新的字符的编码值,最后将高2位置0。例如

src CAAAAAAAAAC

subStr CAAAAAAAAA

int 0100000000

subStr AAAAAAAAAC

int 0000000001

时间复杂度

字符串遍历O(n),hash tableO(1);总时间复杂度O(n)

代码实现

 #include <string>
#include <vector>
#include <unordered_set>
#include <cstring> bool hashMap[*]; class Solution {
public:
std::vector<std::string> findRepeatedDnaSequences(std::string s);
}; std::vector<std::string> Solution::findRepeatedDnaSequences(std::string s) {
std::vector<std::string> rel;
if (s.length() <= ) {
return rel;
} // map char to code
unsigned char convert[];
convert[] = ; // 'A' - 'A' 00
convert[] = ; // 'C' - 'A' 01
convert[] = ; // 'G' - 'A' 10
convert[] = ; // 'T' - 'A' 11 // initial process
// as ten length string
memset(hashMap, false, sizeof(hashMap)); int hashValue = ; for (int pos = ; pos < ; ++pos) {
hashValue <<= ;
hashValue |= convert[s[pos] - 'A'];
} hashMap[hashValue] = true; std::unordered_set<int> strHashValue; //
for (int pos = ; pos < s.length(); ++pos) {
hashValue <<= ;
hashValue |= convert[s[pos] - 'A'];
hashValue &= ~(0x300000); if (hashMap[hashValue]) {
if (strHashValue.find(hashValue) == strHashValue.end()) {
rel.push_back(s.substr(pos - , ));
strHashValue.insert(hashValue);
}
} else {
hashMap[hashValue] = true;
}
} return rel;
}

Leetcode:Repeated DNA Sequences详细题解的更多相关文章

  1. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

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

  2. [Leetcode] Repeated DNA Sequences

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

  3. LeetCode() Repeated DNA Sequences 看的非常的过瘾!

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

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

  5. lc面试准备:Repeated DNA Sequences

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

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

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

  7. 【LeetCode】Repeated DNA Sequences 解题报告

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

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

  9. 【LeetCode】187. Repeated DNA Sequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/repeated ...

随机推荐

  1. WCF - 契约

    契约就是双方或多方就某个问题达成的一种的共识  服务提供者通过契约的形式将服务公布出来 服务消费者根据契约来消费 这样通过契约这个中间者就可以规范服务提供的内容 而服务消费者也可以根据契约来使用服务端 ...

  2. [iOS 开发]UITableView第一行显示不完全

    造成这个问题的原因可能有两个: 1. UITableView的contentOffset属性的改变: 2. MJRefresh调用两次headerEndRefreshing会造成刷新后UITableV ...

  3. 关于this 的一个问题

    var name = "the window"; var object = { name:"my object"; getName:function(){ re ...

  4. 彻底删除mysql的方法(有隐藏文件)

    1.建议使用360进行卸载,可以彻底卸载软件 2.360会提醒删除注册表 3.这个隐藏文件要删除掉 在 C:\Documents and Settings\ 路径下搜索 MySQL 文件夹(默认隐藏的 ...

  5. HTML5 TypeArray和Unicode 字符之间转换

    1.Uint32Array测试成功 // Uint32Array 测试成功 //字符串转为ArrayBuffer对象 function strToab() { var str = '张三丰'; var ...

  6. ToString格式.

    C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...

  7. Encapsulation.

    Access control is often referred to as implementation hiding. Wrapping data and methods within class ...

  8. PL/SQL Developer远程连接Oracle数据库

    首先打开电脑,到pl/sql安装的指定目录[D:\app\DZL\product\11.2.0\dbhome_1\NETWORK\ADMIN]找到[tnsnames.ora]     打开[tnsna ...

  9. [转]Mysql导入导出工具Mysqldump和Source命令用法详解

    Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...

  10. spring jdbctemplate调用procedure(返回游标)

    package cn.com.git.htsc.uac.core.repository.report; import cn.com.git.htsc.uac.core.api.dto.report.R ...