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

    初级 The Newbie Routine 5 minutes hot wrap 5 minutes manual stretch (ten 30-second stretches) 10 minut ...

  2. [译]git remote

    git remote命令让我们可以创建, 查看, 删除一个到其他仓储的连结. 下图展示了我们的本地仓储有两个remote连接, 一个是中央仓储, 一个是其他开发者的仓储. 除了使用完整的url指向他们 ...

  3. IOC和bean容器

  4. resin

    http://blog.csdn.net/sea0x/article/details/6097531 resin 启动: resin 配置文件摘取: <server-default> &l ...

  5. 71 mac boook pro 无 gpu 下caffe 安装

    71 mac boook pro 无 gpu 下caffe 安装 1.首先安装homebrew工具,相当于Mac下的yum或apt ruby -e "$(curl -fsSL https:/ ...

  6. 如何编写可维护的面向对象JavaScript代码

    能够写出可维护的面向对象JavaScript代 码不仅可以节约金钱,还能让你很受欢迎.不信?有可能你自己或者其他什么人有一天会回来重用你的代码.如果能尽量让这个经历不那么痛苦,就可以节省不少时 间.地 ...

  7. SEO 百度后台主动推送链接

    实践步骤,先用爬虫程序将本网站的所有连接爬取出来,再用python文件处理程序把爬虫来的东东整理成一行一个链接的文本格式.再用postman接口测试工具,使用post方式,将所有的链接post过去,这 ...

  8. ASP.NET、C#调用外部可执行exe文件--多种方案

    一. try { //方法一 //调用自己的exe传递参数 //Process proc = new Process(); //proc.StartInfo.FileName = @"D:\ ...

  9. 总结一下classpath

    今天好好研究了一下Java的classpath,什么是classpath呢?classpath就是我们输入 java xxx 的时候Java执行环境搜索xxx类文件的路径.指定这个路径有两种方式,第一 ...

  10. 符瑞艺 160809228_C语言程序设计实验2 选择结构程序设计

    实验2- 输入3个数,并按由大到小的顺序输出. 实验要求: 编写一个C程序,输入3个数,并按由大到小的顺序输出. 参考: 源码: #include <stdio.h> int main() ...