187:重复的DNA序列

This commit is contained in:
huangge1199 2021-10-08 15:29:57 +08:00
parent cd184415b4
commit 4810a4c4f8
4 changed files with 95 additions and 4 deletions

View File

@ -0,0 +1,64 @@
//所有 DNA 都由一系列缩写为 'A''C''G' 'T' 的核苷酸组成例如"ACGAATTCCG"在研究 DNA 识别 DNA 中的重复
//序列有时会对研究非常有帮助
//
// 编写一个函数来找出所有目标子串目标子串的长度为 10且在 DNA 字符串 s 中出现次数超过一次
//
//
//
// 示例 1
//
//
//输入s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
//输出["AAAAACCCCC","CCCCCAAAAA"]
//
//
// 示例 2
//
//
//输入s = "AAAAAAAAAAAAA"
//输出["AAAAAAAAAA"]
//
//
//
//
// 提示
//
//
// 0 <= s.length <= 10
// s[i] 'A''C''G' 'T'
//
// Related Topics 位运算 哈希表 字符串 滑动窗口 哈希函数 滚动哈希 👍 255 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//187:重复的DNA序列
class RepeatedDnaSequences {
public static void main(String[] args) {
//测试代码
Solution solution = new RepeatedDnaSequences().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> result = new ArrayList<>();
Map<String, Integer> cnt = new HashMap<>();
for (int i = 0; i <= s.length() - 10; ++i) {
String sub = s.substring(i, i + 10);
cnt.put(sub, cnt.getOrDefault(sub, 0) + 1);
if (cnt.get(sub) == 2) {
result.add(sub);
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,29 @@
<p>所有 DNA 都由一系列缩写为 <code>'A'</code><code>'C'</code><code>'G'</code><code>'T'</code> 的核苷酸组成,例如:<code>"ACGAATTCCG"</code>。在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助。</p>
<p>编写一个函数来找出所有目标子串,目标子串的长度为 10且在 DNA 字符串 <code>s</code> 中出现次数超过一次。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>输出:</strong>["AAAAACCCCC","CCCCCAAAAA"]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = "AAAAAAAAAAAAA"
<strong>输出:</strong>["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code><code>'A'</code><code>'C'</code><code>'G'</code><code>'T'</code></li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li><li>哈希表</li><li>字符串</li><li>滑动窗口</li><li>哈希函数</li><li>滚动哈希</li></div></div><br><div><li>👍 255</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long