方法一 暴力枚举
var containsPattern = function(arr, m, k) {const n = arr.length;for (let l = 0; l <= n - m * k; ++l) {let offset;for (offset = 0; offset < m * k; ++offset) {if (arr[l + offset] !== arr[l + offset % m]) {break;}}if (offset === m * k) {return true;}}return false;
};
消耗时间和内存情况:
方法二: 计数法
var containsPattern = function(arr, m, k) {let len = arr.length;if(m * k > len) return false;let count = 0, total = m * (k - 1);for(let i = 0; i < len-m; i++) {if(arr[i] === arr[i + m]) {count++;if(count === total) return true;} else {count = 0;}}return false;
};
消耗时间和内存情况:
方法三 正则匹配
var containsPattern = function(arr, m, k) {return new RegExp(`((\\d,){${m}})\\1{${k - 1}}`).test(arr.join(',')+',');
};
消耗时间和内存情况: