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"].

思路I:遍历string,每次截取10个字符,判断出现次数。

Result: Time Limit Exceeded

思路II:字符数较少=>用数字表示字符=>用bitmap来表示字符串,好处:节省空间

比如本题只可能出现4种字符=>可表示为0,1,2,3,即可以用2bits来表示=>字符原本一个字符占1 byte = 8 bits,现在只要2 bits

class Solution {
public:
int getVal(char ch) {
if (ch == 'A') return ;
if (ch == 'C') return ;
if (ch == 'G') return ;
if (ch == 'T') return ;
} vector<string> findRepeatedDnaSequences(string s) {
int sLen = s.length();
unsigned int val=;
char mp[*]={};
vector<string> ret;
string str; if(sLen < ) return ret; for(int i = ; i < ; i++){
val <<=;
val |= getVal(s[i]);
} for(int i = ; i < sLen; i++){
val <<= ;
val |= getVal(s[i]);
val &= 0xFFFFF;
if(++mp[val] == ){
str = s.substr(i-,);
ret.push_back(str);
}
} return ret;
}
};

187. Repeated DNA Sequences (String; Bit)的更多相关文章

  1. [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  2. leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  3. Java for LeetCode 187 Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  4. 187. Repeated DNA Sequences

    题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...

  5. [LeetCode#187]Repeated DNA Sequences

    Problem: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...

  6. [LeetCode] 187. Repeated DNA Sequences 解题思路

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  7. 【LeetCode】187. Repeated DNA Sequences

    题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...

  8. 187. Repeated DNA Sequences重复的DNA子串序列

    [抄题]: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &qu ...

  9. *187. Repeated DNA Sequences (hashmap, one for loop)(difference between subsequence & substring)

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. leetcode998

    class Solution: def __init__(self): self.prelist = list() def preTree(self,root:TreeNode): if root ! ...

  2. 20165304《Java程序设计》第五周学习总结

    教材学习内容总结 第七章 1.内部类 注意内部类和外嵌类的关系: 外嵌类的成员变量和方法在内部类有效 内部类的类体不可以声明static变量和方法.外嵌类的类体可以用内部类声明对象. 内部类仅供它的外 ...

  3. PHP中汉字截取

    $len = 19; $text = "怎么将新闻的很长的标题只显示前面一些字,后面用.....来代替?"; echo strlen($text)<=$len ? $text ...

  4. upcast

    class A { public: A():i(){} int get_i() { cout << "A.get_i" << endl; return i; ...

  5. js 对象创建设计模式

    创建js对象可以使用多种模式,每种模式有着不同的特点:如下: 1.工厂模式:创建一个函数,在函数中实例化一个对象,当每次调用函数时,就实例化一个对象,并返回这个对象: 我们知道,对象是引用形式的,每次 ...

  6. aaad

    I remember the wonderful moment you appeared before me, like a fleeting vision, like a genius of pur ...

  7. 4:list 列表

    list:列表.数组.array . list 是有序的,list的定义以 [] 为标识.如:list1 = ['name1', 'name2', 'name3'] 元素可以是任何类型的,如字符串.数 ...

  8. zTree插件 角色、部门、人员分类选择

    // 传参数调用 function test(){roleOrOrgSelect(3,function(data){console.log(data);});} /** * * @param type ...

  9. 吴裕雄 python 机器学习-NBYS(2)

    import matplotlib import numpy as np import matplotlib.pyplot as plt n = 1000 xcord0 = [] ycord0 = [ ...

  10. Linux部署项目

    1 安装jdk 第一步:获取Linux系统中jdk安装包和tomcat安装包(后面要用,所以上传两个) 第二步:使用secureCRT客户端工具连到服务器 第三步:使用命令创建一个目录,作为软件的安装 ...