【LeetCode】187. Repeated DNA Sequences
2016-03-22 14:14
369 查看
题目:
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"].
提示:
这道题最初的想法就是使用HashMap来做,一开始的时候用了STL里的unordered_map:
class Solution { public: vector<string> findRepeatedDnaSequences(string s) { unordered_set<string> string_set; if (s.length() <= 10) { return {}; } unordered_map<string ,int> m; for (int i = 0; i < s.length() - 9; ++i) { string tmp = s.substr(i, 10); if (m.find(tmp) == m.end()) { m[tmp] = 1; } else { string_set.insert(tmp); } } return vector<string>(string_set.begin(), string_set.end()); } };
做下来以后虽然AC了,但是时间却很不理想,原因也很简单,对于这种特定的问题,使用自定义的map和hash方法会让算法的效率得到很大的提高,之于如何设计map和hash的问题,我们不妨先看一看这个问题输入所具有的特点:
输入的字符串长度是固定的(10个字符)
字符只有{'A', 'C', 'T', 'G'}四种
结合上述两点,可以发现输入的可能性是可以计算出来的,即4^10个。
这样一来,我们就可以利用一个基本类型的数组作为map,而hash的算法则是可以想办法把4种字符分别映射到[1,4]这4个数字上,具体的做法可以看代码。
代码:
class Solution { public: vector<string> findRepeatedDnaSequences(string s) { // 将该char数组作为一个map char map[1048576] = {0}; int len = s.length(), num = 0; vector<string> res; if (len <= 10) { return res; } for (int i = 0; i < 9; ++i) { num <<= 2; // 可以试一下下面的计算方法,效果就是把4个字符映射到了1,2,3,4这四个数字上 num |= (s[i] - 'A' + 1) % 5; } for (int i = 9; i < s.length(); ++i) { num <<= 2; num |= (s[i] - 'A' + 1) % 5; // 0xfffff代表了20个1组成的二进制序列,按位与之后,结果就是当前字符串代表的数值 num = num & 0xfffff; if (map[num]++ == 1) { res.push_back(s.substr(i - 9, 10)); } } return res; } };
相关文章推荐
- 程序导入友盟微社区后报错android.view.InflateException:com.umeng.comm.ui.imagepicker.widgets.RoundImageView
- Server 2012 Core 到Full Gui的转换
- Activity中UI框架基本概念
- 修改EasyUI中DateBox和DateTimeBox的格式
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问
- :The server quit without updating PID file问题的解决办法
- lintcode-medium-Implement Queue by Two Stacks
- 配置HUE 遇到Error in sasl_client_start (-4) SASL(-4): no mechanism available: No worthy mechs found
- Java String、StringBuilder和StringBuffer
- Invalid byte 1 of 1-byte UTF-8 sequence终极解决方案
- UIImageView或是UIButton怎样画环
- Errors occurred during the build.
- java设计模式4--建造者模式(Builder)
- ZwQuerySystemInformation中的结构体和例子
- iOS SDK具体解释之UIDevice(系统版本号,设备型号...)
- 第十八讲--检查点队列(checkpoint queue)
- 文本视图 UITextView
- Request、Request.Form和Request.QueryString的区别
- ReactiveCocoa代码实践之-UI组件的RAC信号操作
- 国内UED收录