题解 | #DNA序列#
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let s = ''
let n = 0
let row = 0
rl.on('line', function (line) {
row++
if(row === 1){
// 保存要操作的字符串
s = line
}else {
// 保存子串长度
n = Number(line)
// 使用功能滑动窗口法遍历
let left = 0
let right = left + n - 1
let max = 0
// 记录窗口左右的下标,以便后期截取子串
let [i,j] = [left, right]
while(right < s.length){
// 计算cg的数量
let num = 0
for(let i = left; i <= right; i++){
if(s[i] === 'C' || s[i] === 'G') num++
}
if(num/s.length > max){
max = num/s.length;
[i,j] = [left, right]
}
left++
right++
}
console.log(s.slice(i,j +1))
}
});
查看5道真题和解析