当前位置:网站首页>Repeated DNA sequences for leetcode topic resolution
Repeated DNA sequences for leetcode topic resolution
2022-06-23 06:17:00 【ruochen】
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".
Review bitmap . Bitwise operation ,A C G T Use the following bits Express :
A 00 C 01 G 10 T 11
therefore 10 Two consecutive characters , It only needs 20 Bit representation , And one int(32 position ) I can represent . Defining variables hash, after 20 Bit represents a sequence of strings , Other digits 0 .
Define a set Used to store what has already appeared hash, New calculation hash when , If there has been , Just put the result set in .
public List<String> findRepeatedDnaSequences(String s) {
if (s == null || s.length() < 11) {
return new ArrayList<String>();
}
int hash = 0;
Set<Integer> appear = new HashSet<Integer>();
Set<String> set = new HashSet<String>();
Map<Character, Integer> map = new HashMap<Character, Integer>();
map.put('A', 0);
map.put('C', 1);
map.put('G', 2);
map.put('T', 3);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
hash = (hash << 2) + map.get(c);
hash &= (1 << 20) - 1;
if (i >= 9) {
if (appear.contains(hash)) {
set.add(s.substring(i - 9, i + 1));
} else {
appear.add(hash);
}
}
}
return new ArrayList<String>(set);
}边栏推荐
- Android handler memory leak kotlin memory leak handling
- Pyinstaller sklearn reports errors
- Find the number of nodes in the widest layer of a binary tree
- Pat class B 1020 Moon Cake
- Kotlin collaboration +retro most elegant network request use
- The hierarchyviewer tool cannot find the hierarchyviewer location
- Cloud native database is the future
- Day_11 传智健康项目-图形报表、POI报表
- Gplearn appears assignment destination is read only
- 【Leetcode】431. Encode n-ary tree to binary tree (difficult)
猜你喜欢

Day_12 传智健康项目-JasperReports

Activity startup mode and life cycle measurement results

New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!

The hierarchyviewer tool cannot find the hierarchyviewer location

ant使用总结(三):批量打包apk

Efficient office of fintech (I): automatic generation of trust plan specification

Explicability of counter attack based on optimal transmission theory

jvm-05. garbage collection

Pyqt5 setting window top left Icon

【Leetcode】431. Encode n-ary tree to binary tree (difficult)
随机推荐
App SHA1 acquisition program Baidu map Gaode map simple program for acquiring SHA1 value
Day_10 传智健康项目-权限控制、图形报表
Kotlin Android simple activity jump, simple combination of handler and thread
Fraction to recursing decimal
微软面试题:打印折纸的折痕
使用aggregation API扩展你的kubernetes API
[DaVinci developer topic] -42- how to generate template and header files of APP SWC
Pat class B 1011 C language
matplotlib savefig多个图片叠加问题
金融科技之高效办公(一):自动生成信托计划说明书
Pyinstaller sklearn报错的问题
Redis sentry
Causes and methods of exe flash back
Given a node of a binary tree, return the successor node of the node
SQL表名与函数名相同导致SQL语句错误。
去除防火墙和虚拟机对live555启动IP地址的影响
Memory analysis and memory leak detection
jvm-04.对象的内存布局
Tcp/ip explanation (version 2) notes / 3 link layer / 3.3 full duplex, energy saving, automatic negotiation mechanism, 802.1x flow control / 3.3.3 link layer flow control
About the error of installing PIP3 install chatterbot