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. ThikPHP3.1 常用方法(one)

    公司常用但没学过的一些函数,记录一下备份. 1,在Rest操作方法中,可以使用$this->_type获取当前访问的资源类型,用$this->_method获取当前的请求类型. 2.uns ...

  2. 用SQLite查看编辑android导出的微信聊天记录

    上一篇我们已经能够完成文字版微信聊天记录导出android了,也即复制或剪切MicroMsg.db文件到电脑,以.db格式结尾的文件是数据库文件(database document),需要安装相关数据 ...

  3. springmvc之定时器

    一.通过注解方式实现定时器 1.工程结构 2.所需jar包 3.spring-config.xml,springmvc配置文件 <?xml version="1.0" enc ...

  4. js搜索框输入提示(高效-ys8)

    <style type="text/css"> .inputbox .seleDiv { border: 1px solid #CCCCCC; display: non ...

  5. (苹果AppleWWDRCA.cer证书过期)Failed to locate or generate matching signing assets

    从2月14号开始,上传AppStore会碰到:Failed to locate or generate matching signing assets 字数462 阅读13571 评论16 喜欢61 ...

  6. Visual Studio error C2001:常量中有换行符(解决办法)

    在Visual Studio自动生成的项目中,碰见了一件关于文件编码的问题,集中在类似于以下的语句上: DASLog (DASProtWarn, L"(%s)消息超时,进入慢循环召唤模式.& ...

  7. JavaScript获取onclick、onchange等事件值的代码

    这里主要是用到了getAttributeNode()这个方法,它获取的是属性节点,忽略属性和事件的差别,具体示例如下,感兴趣的朋友可以参考下哈希望对大家有所帮助 今天小菜处理下拉菜单级联问题时,想获取 ...

  8. tableView 选中cell时,获取到当前cell

    // >> 找到当前选中的cell,设置选中时的cell背景色 SideTableViewCell * cell = (SideTableViewCell *)[tableView cel ...

  9. SQL语句在查询分析器中可以执行,代码中不能执行

    问题:SQL语句在查询分析器中可以执行,代码中不能执行 解答:sql中包含数据库的关键字,将关键字用[]括起来,可以解决. 后记:建数据库的时候尽量避免使用关键字. 例子: sql.Format(&q ...

  10. django formset bug?

    碰到了一个郁闷的问题,修改inlineformset时,全部删掉子表,再新增一行时,报错. 背景: 用django配合jq做动态表格,实现用js动态添加/删除行,并通过inlineformset更新到 ...