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.

For example,

Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",

Return:
["AAAAACCCCC", "CCCCCAAAAA"].

思路:

开始用hash表存储所有出现过一次的字符串,结果空间超了。 有用最简单的循环,时间又超了。 做不出来,看答案。

大神的方法,思路是用一个整数来表示一个10字符长的字符串,相当于给字符串编码了。每个字母用一个 2位的二进制数表示 依次把每位对应的数字左移,后面或上新的表示数字。

//大神的方法 思路是用一个整数来表示一个10字符长的字符串 相当于给字符串编码了
vector<string> findRepeatedDnaSequences3(string s) {
unordered_set<int> words;
vector<string> ans;
char* map = new char[];
map['A' - 'A'] = ; //A C G T 分别用二进制数 00 01 10 11表示
map['C' - 'A'] = ;
map['G' - 'A'] = ;
map['T' - 'A'] = ; for(int i = ; i + < s.length(); i++) //遍历所有起始位置 注意!!! 必须写成i + 9 < s.length() 不能写成 s.length() - 9, 因为s.length()-9为负数时会被当做是大正数,即并没有用负数来表示。 可能是s.length()是无符号数的原因
{
int v = ;
for(int j = i; j < i + ; j++)
{
//对于一个字符串,每一个字母对应一个两位的二进制数 每次把数字左移两位 留出新的空位来放新字母对应的数
v <<= ;
v |= map[s[j] - 'A'];
}
//如果数字已经出现过,并且还没有被放入答案中,压入答案
if(words.find(v) != words.end() && find(ans.begin(), ans.end(), s.substr(i, )) == ans.end())
{
ans.push_back(s.substr(i, ));
}
else
{
words.insert(v);
}
} return ans;
}

我的两个通不过的方法

//hash表 内存超了
vector<string> findRepeatedDnaSequences(string s) {
vector<string> ans;
unordered_set<string> hash; if(s.length() < ) return ans; for(int i = ; s.length() - i - >= ; i++)
{
string sub = s.substr(i, );
if(find(ans.begin(), ans.end(), sub) != ans.end())
{
continue;
}
if(hash.count(sub) == )
{
hash.insert(sub);
}
else
{
hash.erase(sub);
ans.push_back(sub);
}
}
return ans; } //简单的查找法 时间超了
vector<string> findRepeatedDnaSequences2(string s) {
vector<string> ans;
if(s.length() < ) return ans; for(int i = ; s.length() - i - >= ; i++)
{
string sub = s.substr(i, );
if(find(ans.begin(), ans.end(), sub) != ans.end())
{
continue;
}
else if(s.find(sub, i + ) != s.npos)
{
ans.push_back(sub);
}
} return ans;
} //大神的方法 思路是用一个整数来表示一个10字符长的字符串 相当于给字符串编码了
vector<string> findRepeatedDnaSequences3(string s) {
unordered_set<int> words;
vector<string> ans;
char* map = new char[];
map['A' - 'A'] = ; //A C G T 分别用二进制数 00 01 10 11表示
map['C' - 'A'] = ;
map['G' - 'A'] = ;
map['T' - 'A'] = ; for(int i = ; i + < s.length(); i++) //遍历所有起始位置
{
int v = ;
for(int j = i; j < i + ; j++)
{
//对于一个字符串,每一个字母对应一个两位的二进制数 每次把数字左移两位 留出新的空位来放新字母对应的数
v <<= ;
v |= map[s[j] - 'A'];
}
//如果数字已经出现过,并且还没有被放入答案中,压入答案
if(words.find(v) != words.end() && find(ans.begin(), ans.end(), s.substr(i, )) == ans.end())
{
ans.push_back(s.substr(i, ));
}
else
{
words.insert(v);
}
} return ans;
}

【leetcode】Repeated DNA Sequences(middle)★的更多相关文章

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

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

  2. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. 【leetcode】Compare Version Numbers(middle)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  4. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  6. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  7. 【leetcode】Set Matrix Zeroes(middle)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...

  8. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  9. 【leetcode】 search Insert Position(middle)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

随机推荐

  1. nodejs开发指南读后感

    nodejs开发指南读后感 阅读目录 使用nodejs创建http服务器; supervisor的使用及nodejs常见的调式代码命令了解; 了解Node核心模块; ejs模板引擎 Express 理 ...

  2. Ajax中的get和post两种请求方式的异同

    Ajax中我们经常用到get和post请求.那么什么时候用get请求,什么时候用post方式请求呢? 在做回答前我们首先要了解get和post的区别.   1. get是把参数数据队列加到提交表单的A ...

  3. SqlServer代理执行[分发清除: distribution] 无法删除快照文件

    每天偶尔检查数据库作业是否失败,发现有错误 select top 10 job_id,run_date,run_time,run_duration,step_name,message from  ms ...

  4. [译]Mongoose指南 - Connection

    使用mongoose.connect()方法创建连接 mongoose.conect('mongodb://localhost/myapp'); 上面的代码是通过默认端口27017链接到mongodb ...

  5. HTML Table导出为Excel的方法

    HTML Table导出为Excel的方法: 直接上源码 <html> <head> <meta http-equiv="Content-Type" ...

  6. Ruby学习之module

    我们可以认为module是一个专门存放一系列方法和常量的工具箱. module和class非常像, 只是module不能创建实例也不能有子类, 它们仅仅能存放东西. 例如: module Circle ...

  7. POJ 3252 Round Numbers

     组合数学...(每做一题都是这么艰难) Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7607 A ...

  8. CodeForces 55D Beautiful numbers

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  9. js跳转页面

    <script type="text/javascript">  方法一: location.href = 'http://www.baidu.com'; 方法二: l ...

  10. STM32F10xx CAN BUS相关库文件"stm32f10x_can.c"内的库函数解析

    一.背景: 还是继续CAN通信,要节省开发时间,使用库函数可大大降低开发周期,并且还能确保寄存器的配置几 乎是万无一失,所以,在此就STM32F10xx的CAN操作库函数的使用做个简析. STM32有 ...