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: "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"].
其实就是一个字符串,然后以10个为单位,求重复两次以上的字符串。
1、用一个set就可以实现了。
public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> list = new ArrayList();
int len = s.length();
if (len <= 10){
return list;
}
HashSet<String> set = new HashSet();
for (int i = 10; i <= len; i++){
String str = s.substring(i - 10, i);
if (set.contains(str) && !list.contains(str)){
list.add(str);
} else {
set.add(str);
}
}
return list;
}
}
2、discuss里面是有一些利用位操作的,例如。
public List<String> findRepeatedDnaSequences(String s) {
Set<Integer> words = new HashSet<>();
Set<Integer> doubleWords = new HashSet<>();
List<String> rv = new ArrayList<>();
char[] map = new char[26];
//map['A' - 'A'] = 0;
map['C' - 'A'] = 1;
map['G' - 'A'] = 2;
map['T' - 'A'] = 3; for(int i = 0; i < s.length() - 9; i++) {
int v = 0;
for(int j = i; j < i + 10; j++) {
v <<= 2;
v |= map[s.charAt(j) - 'A'];
}
if(!words.add(v) && doubleWords.add(v)) {
rv.add(s.substring(i, i + 10));
}
}
return rv;
}
leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java的更多相关文章
- [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 ...
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [leetcode]187. Repeated DNA Sequences寻找DNA中重复出现的子串
很重要的一道题 题型适合在面试的时候考 位操作和哈希表结合 public List<String> findRepeatedDnaSequences(String s) { /* 寻找出现 ...
- [LeetCode] 187. Repeated DNA Sequences 解题思路
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 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 ...
- [LeetCode#187]Repeated DNA Sequences
Problem: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: ...
- LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)
187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...
- 【Leetcode】【Medium】Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- Java实现 LeetCode 187 重复的DNA序列
187. 重复的DNA序列 所有 DNA 都由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对 ...
随机推荐
- 用PowerMock mock static方法
在编写代码时,经常需要调用别人已经写好的工具类,而这些工具提供的方法经常是static方法,在这里,直接贴出<PowerMock实战手册>中的例子 待测试方法: public class ...
- Fix the iOS code signing issue when using Jenkins
This week I setup the Jenkins on my Mac and try to build iOS applications. unfortunately I got the c ...
- linux查看系统命令
[root@server ~] # uname –a # 查看内核/操作系统/CPU信息的linux系统信息命令 [root@server ~] # hea ...
- iOS学习之block
Block是C语言的扩充功能.带有自动变量(局部变量)的匿名函数.(不带有名称的函数) 非匿名函数:int func(int count):(声明了名称为func的函数)使用:int result = ...
- docker push 实现过程
这一篇文章分析一下docker push的过程:docker push是将本地的镜像上传到registry service的过程: 根据前几篇文章,可以知道客户端的命令是在api/client/pus ...
- # mysql -u root -p -bash: mysql: command not found
[root@jboss ~]# mysql -u root -p-bash: mysql: command not found 需要安装mysql # yum install mysql之后就行 了
- 如何利用Matlab进行ROC分析
ROC曲线基本知识: 判断分类器的工作效率需要使用召回率和准确率两个变量. 召回率:Recall,又称"查全率", 准确率:Precision,又称"精度".& ...
- linux下解压war文件命令
jar -xvf project.war -->解压到当前目录下. -f 指定 JAR 文件名,通常这个参数是必须的 -v 显示过程信息
- 分享自己配置的HttpURLConnection请求数据工具类
>>该工具类传入string类型url返回string类型获取结果import java.io.BufferedReader;import java.io.InputStream;impo ...
- SpringMVC之入门
Spring MVC简介 Spring MVC:Spring MVC也叫Spring Web MVC,属于表现层框架,是Spring中的一份子. Spring MVC执行流程图 第一个SpringMV ...