题目

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. cocos2dx实现android的对讯飞语音的合成(语言朗读的实现)

    事实上非常easy,只是有些细节须要注意. 关于讯飞语音在android上的应用,大家须要自己去下载SDK,然后依照讯飞语音提供的api在自己的android的Demo上执行成功,那东西也相当的简单. ...

  2. linux文件系统和mount(硬盘,win分区,光驱,U盘)

    fdisk –l查看dos/win/ext2分区(partiton,不是slice,slice是solaris分区) [root@localhost etc]# /sbin/fdisk -l Disk ...

  3. Java基础知识强化之集合框架笔记33:Arrays工具类中asList()方法的使用

    1. Arrays工具类中asList()方法的使用 public static <T> List<T> asList(T... a): 把数组转成集合 注意事项: 虽然可以把 ...

  4. insert当 sql语句里面有变量 为字符类型的时候 要3个单引号

    set @InsertStr='INSERT INTO [dbo].[T_SchoolPercentMonth]([SchoolID],[MonthOfYear],[PercentNum]) VALU ...

  5. SQL server 使用触发器跨数据库备份数据

    create database TriggerTest create table transInfo2 --交易信息表 ( cardID ) not null, --卡号 transType ) no ...

  6. oracle中获取特定时间的前一天

    select to_char(to_date('@rq','YYYY-MM-DD')-1,'YYYY-MM-DD') FROM DUAL 把@rq换成你要的时间就行了

  7. Deep Learning学习随记(二)Vectorized、PCA和Whitening

    接着上次的记,前面看了稀疏自编码.按照讲义,接下来是Vectorized, 翻译成向量化?暂且这么认为吧. Vectorized: 这节是老师教我们编程技巧了,这个向量化的意思说白了就是利用已经被优化 ...

  8. UITextView/UITextField检测并过滤Emoji表情符号

    UITextView/UITextField检测并过滤Emoji表情符号 本人在开发过程中遇到过这种情况,服务器端不支持Emoji表情,因此要求客户端在上传用户输入时,不能包含Emoji表情.在客户端 ...

  9. PictureBox内的图片拖动功能

    当 PictureBox内的图片太大,超过PictureBox边框时可以用下面的方法来实现,   通过重绘来实现 :   Code bool wselected = false;  Point p = ...

  10. (JAVA)从零开始之--对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)

    对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流:  Ob ...