187. Repeated DNA Sequences
2016-05-09 17:02
387 查看
Problem
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”].
Solution
Discuss中的回答“A”,“C”,“G”,“T”的ASCII码分别是65,67,71和84,转换成二进制分别是01000001,01000011,01000111,01010100,可以看到他们的最后三位是不一样的。所以只需要用最后三位就可以区别这四个字母。
class Solution { public: vector<string> findRepeatedDnaSequences(string s) { unordered_map<int,int> map; vector<string> ret; int key = 0; for(int i = 0;i<s.length();++i) { key = ((key<<3)|(s[i] & 0x7)) & 0x3FFFFFFF; if(i<9) continue; if(map.find(key) == map.end()) map[key] = 1; else if(map[key] == 1) { ret.push_back(s.substr(i-9,10)); map[key]++; } } return ret; } };
相关文章推荐
- 布局+基本UI
- JS 生成GUID 方法
- UILabel设置行间距
- javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available..
- iOS UIImageView 的使用
- 187. Repeated DNA Sequences 【M】【30】
- iOS 自定义UIButton的title和image位置
- 第十章 UINavigationController 翻译
- 数据库重构工具 LiquiBase(5 Liquibase被锁)
- Liquibase被锁
- 集合框架--Map集合values
- ArrayList的<STRING>到CharSequence的[]
- EqualsBuilder和HashCodeBuilder 作用
- iOS--UITableViewCell去除重影的方法
- Moqui学习之 Step by Step OrderProcureToPayBasicFlow
- 利用quic协议访问google
- UIView与CALayer
- 简单实现跑马灯效果
- 自己写的一个php基于phpQuery的通用采集类
- UIDatePicker转为汉字,并且显示年月日