Leetcode之Repeated DNA Sequences

早过忘川 提交于 2019-11-30 13:21:05

题目:

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.

Example:

Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"

Output: ["AAAAACCCCC", "CCCCCAAAAA"]

代码:

方法一——map:

class Solution {
public:
 vector<string> findRepeatedDnaSequences(string s) {
	int len = s.length();
	map<string, int> m;
	vector<string> res;

	for (int i = 0; i < len - 9; i++) {
		string t = s.substr(i, 10);
		if (!m.count(t)) {
			m[t] = 1;
		}
		else {
			if(find(res.begin(),res.end(),t)==res.end())
				res.push_back(t);
		}
	
	}
	return res;
}
};

方法二——位操作:

class Solution {
public:
 vector<string> findRepeatedDnaSequences(string s) {
	unordered_set<int> seen;
        unordered_set<int> dup;
        vector<string> result;
        vector<char> m(26);
        m['A' - 'A'] = 0;
        m['C' - 'A'] = 1;
        m['G' - 'A'] = 2;
        m['T' - 'A'] = 3;
         
        for (int i = 0; i + 10 <= s.size(); ++i) {
            string substr = s.substr(i, 10);
            int v = 0;
            for (int j = i; j < i + 10; ++j) { //20 bits < 32 bit int
                v <<= 2;
                v |= m[s[j] - 'A'];
            }
            if (seen.count(v) == 0) { //not seen
                seen.insert(v);
            } else if (dup.count(v) == 0) { //seen but not dup
                dup.insert(v);
                result.push_back(substr);
            } //dup
        }
        return result;
}
};

想法:

多看别人做的

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!