我正在研究黑客等级问题。 重复的字符串 [1]:https://www.hackerrank.com/challenges/repeated-string/problem如何处理javascript中for循环的大量内部?
function main() {
var s = readLine();
var n = parseInt(readLine());
var rep = 0;
var repArray = []
//calculate each case
while(repArray.length < n){
for(let j = 0; j < s.length; j++){
if(repArray.length > n){
break;
}
repArray.push(s[j])
}
}
for(let a = 0; a < repArray.length; a++){
if(repArray[a] === 'a'){
rep++
}
}
console.log(rep)
}
我接收到错误的输入 一个 万亿
我的代码的输出是 < ---过去几年的GC --->
1836 ms: Mark-sweep 597.4 (605.3) -> 359.7 (368.6) MB, 101.7/0.0 ms [allocation failure] [GC in old space requested].
1938 ms: Mark-sweep 359.7 (368.6) -> 359.7 (368.6) MB, 102.3/0.0 ms [allocation failure] [GC in old space requested].
2040 ms: Mark-sweep 359.7 (368.6) -> 359.7 (367.6) MB, 101.6/0.0 ms [last resort gc].
2142 ms: Mark-sweep 359.7 (367.6) -> 359.7 (367.6) MB, 101.7/0.0 ms [last resort gc].
< --- JS堆栈跟踪--->
==== JS堆栈跟踪=========================================
Security context: 0x10178c2cfb51 2: main [/run-N6KBYU8cQzCneXKH0Tbm/solution.js:~30] [pc=0x2859725aec0] (this=0x10178c2e6119) 3: /* anonymous */ [/run-N6KBYU8cQzCneXKH0Tbm/solution.js:21] [pc=0x2859725717e] (this=0x2af8d3a77e81) 4: emitNone(aka emitNone) [events.js:91] [pc=0x28597256c33] (this=0x10178c204381 ,handler=0x2af8d3a78049 ,is...
这段代码的错误是
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 1: node::Abort() [/usr/local/nodejs-binary/bin/node] 2: 0x1098b2c [/usr/local/nodejs-binary/bin/node] 3: v8::Utils::ReportApiFailure(char const*, char const*) [/usr/local/nodejs-binary/bin/node] 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/usr/local/nodejs-binary/bin/node] 5: v8::internal::Factory::NewUninitializedFixedArray(int) [/usr/local/nodejs-binary/bin/node] 6: 0xc4553f [/usr/local/nodejs-binary/bin/node] 7: v8::internal::Runtime_GrowArrayElements(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/nodejs-binary/bin/node] 8: 0x285971079a7
提示:这个问题可以不用实际生成和遍历字符串来解决。 – Quelklef
这个挑战看起来像C,是将C转换成JavaScript的挑战吗? – zer00ne