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. 找不到类型“{x}.{x}”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。

    最近在搞一个WCF的项目... 刚开始在这条路上走... 各种崎岖... 网上搜到的一种解决方案(也是大多数情况的解决方案): 原文:http://www.cnblogs.com/Olive116/p ...

  2. 【Bootstrap】Bootstrap和Java分页-第一篇

    目录 关于此文 pagination BetweenIndex DefaultPagination QueryHandler BookDaoImpl BookServiceImpl BookActio ...

  3. 认识ATL窗口

    这是一个相当于“Hello world!”的任务,作为认识ATL,考查了其运作流程与机制. 环境:VS2008 创建:新建-项目-Win32项目-添加公用头文件用于(选择ATL). PS:注意新建项目 ...

  4. 免费的HTML5连载来了《HTML5网页开发实例详解》连载(二)

    最近新浪.百度.腾讯.京东.大众点评.淘宝等流行的网站都加大了招聘HTML5的力度,HTML5开发人员成了抢手货,本次连载的是由大众点评前端工程师和一淘网前端工程师基情奉献的<HTML5网页开发 ...

  5. myeclipse的项目导入到eclipse下,com.sun.org.apache.commons.beanutils.BeanUtils不能导入

    com.sun.org.apache.commons.beanutils.BeanUtils这个包不能引入了怎么办自己下了个org.apache.commons的jar包了之后,改成import or ...

  6. 兼容ie的jquery ajax文件上传

    Ajax文件上传插件很多,但兼容性各不一样,许多是对ie不兼容的,另外项目中是要求将网页内容嵌入到桌面端应用的,这样就不允许带flash的上传插件了,如:jquery uploadify...悲剧 对 ...

  7. HTMLDOM中三种元素节点、属性节点、文本节点的测试案例

    HTML dom中常用的三种节点分别是元素节点.属性节点.文本节点. 具体指的内容可参考下图: 以下为测试用例: <!DOCTYPE html> <html> <head ...

  8. 08OC之Foundation框架

    1.Foundation框架简述 在前面,我们创建一个类的时候,都会选择Cocoa Class.到底Cocoa Class是什么东西呢? Cocoa 不是一门编程语言,因为它可以运行在多种编程语言上, ...

  9. BZOJ1367——[Baltic2004]sequence

    1.题目大意:给一个序列t,然后求一个序列z,使得$|z1-t1|+|z2-t2|+...+|zn-tn|$的值最小,我们只需要求出这个值就可以了,并且z序列是递增的 2.分析:这道题z序列是递增的, ...

  10. python 输入和输出

    到目前为止我们遇到过两种输出值的方法: 表达式语句和print语句. (第三个方式是使用文件对象的write()方法: 标准输出文件可以引用 sys.stdout.详细内容参见库参考手册. Pytho ...