我正在研究黑客等級問題。 重複的字符串 [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