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. 工具介绍 - NimbleText

    非常实用的工具, 即使不是程序员也有必要掌握这个简单的小工具. 这个工具有桌面版和在线版两个版本. 桌面版地址: http://nimbletext.com/ 在线版地址: http://nimble ...

  2. PHP魔术方法以及关于独立实例与相连实例的讲解

    <?php //魔术方法 //当包含多个类 //1.自动装载类的魔术方法__autoload() function __autoload($classname){ if (isset($clas ...

  3. OC第四节——NSDictionary和NSMutableDictionary

    NSDictionary    1.什么是字典        字典是也是一种集合结构,功能与我们现实中的字典工具一样    2.字典的元素是什么        任意类型的对象地址构成键值对    3. ...

  4. PHP get_class 返回对象的类名

    get_class (PHP 4, PHP 5) get_class — 返回对象的类名 说明 string get_class ([ object $obj ] ) 返回对象实例 obj 所属类的名 ...

  5. JAVA-多屏幕显示

    以下代码适用于:一台主机连接多台显示器,JAVA Swing窗口需要分别显示到对应的显示器上. GraphicsEnvironment env = GraphicsEnvironment.getLoc ...

  6. Swift2.1 语法指南——高级操作符

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  7. xcode7 NSAppTransportSecurity

    在Info.plist中添加  NSAppTransportSecurity 类型  Dictionary Dictionary 下添加  NSAllowsArbitraryLoads 类型 Bool ...

  8. BZOJ1861——book

    就是给你一摞书,然后又询问第k大,编号为x的书是第几大,然后修改书的位置 splay模板题...然而我还是不会,,,又遇到lrj的书的坑了,rj的书里没有father,顿时弄得我ask不会搞了 注意合 ...

  9. ios7 ios8导航栏透明

    自动调整scrollview的insets为0, 然后scrollview就不会向下偏移64px self.automaticallyAdjustsScrollViewInsets = NO; 导航栏 ...

  10. native2ascii.exe

    native2ascii.exe 是 Java 的一个文件转码工具,是将特殊各异的内容 转为 用指定的编码标准文体形式统一的表现出来,它通常位于 JDK_home\bin 目录下,安装好 Java S ...