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 中的重复序列有时会对 ...
随机推荐
- qt 单文档程序关闭时在delete ui处出现segmentation fault
做了个显示图片的单文档程序. qt 单文档程序关闭时在delete ui处出现segmentation fault. 调试发现调用两次mainwindow析构函数. http://blog.csdn. ...
- 使用GridVIew显示Gantt(甘特图),动态增减列
说明:本例是做了工厂的排机报表 一.根据查询日期初始化GridView列 private void IniGridView(DateTime p_DateS,DateTime p_DateE) { / ...
- java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException
问题描述: 严重: IOException while loading persisted sessions: java.io.WriteAbortedException: writing abort ...
- TCP Socket 通讯(客户端与服务端)
/*----------------------------编译环境:VS2015---------------------------------------*/ /*--------------- ...
- C++Primer 一
1.vertor和数组的区别: 数组的长度固定.而且程序是无法知道一个给定数组的长度,数组没有获取器容量大小的size操作,也不提供puch_back操作在其中自动添加元素 2.数组定义中的类型可以示 ...
- CSS3的透明度使用
大家在敲代码的时候总会遇见一个问题.就是透明度opacity 会导致整个元素内全部都会改变,通常的方法是在同级元素后面再加上一个元素标签,但是现在有CSS3 ,我们完全不用这么做了.看代码 <! ...
- 使用PHPExcel导出文件
使用PHPExcel导出文件步骤及解析: 新建一个excel表格:实例化PHPExcel类 创建sheet(内置表):createSheet()方法,创建新的sheet方法 setActiveShee ...
- mariadb配置允许远程访问方式
首先配置允许访问的用户,采用授权的方式给用户权限 1 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRAN ...
- org.springframework.beans.MutablePropertyValues.add
最近在项目中,通过maven引入其他包时,启动报错如下: [ERROR][2016-10-08 14:01:20.716]Context initialization failed[org.sprin ...
- c# System.Data.OracleClient需要Oracle客户端软件8.1.7或更高版本
前几天遇到了这个问题,情景是与oracle数据库连接的时候出现的.本机已经安装了客户端,使用toad数据库工具能够与数据库相连进行相关的操作.但是在使用代码进行连接的时候出现了这样的问题.找了好久,都 ...