在Leetcode上面遇到一个题---求一维组的动态和导致内存溢出

2020-12-22 12:26发布

var ARR=[1,2,3,4];

var runningSum = function(nums) {
var arr=[];
for(var i=0;i<nums.length;i++){
if(i=0){
arr.push(nums[i]);
}else{
arr.push(nums[i]+nums[i-1]);
}
}

console.log(arr);

};

runningSum (ARR);

=下面报错======

PS C:\Users\PC-17\Desktop\trash\12.16_1\pagination> node app.js

<--- Last few GCs --->

[15272:000002322F3D55B0] 4199 ms: Scavenge 670.0 (704.3) -> 670.0 (704.3) MB, 0.3 / 0.0 ms (average mu = 0.996, current mu = 0.996) allocation failure
[15272:000002322F3D55B0] 6019 ms: Scavenge 1052.5 (1086.8) -> 1052.5 (1086.8) MB, 0.4 / 0.0 ms (average mu = 0.996, current mu = 0.996) allocation failure
[15272:000002322F3D55B0] 8988 ms: Scavenge 1626.3 (1660.6) -> 1626.3 (1660.6) MB, 0.4 / 0.0 ms (average mu = 0.996, current mu = 0.996) allocation failure

<--- JS stacktrace --->

FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory
1: 00007FF7B63003EF napi_wrap+110031
2: 00007FF7B62A6106 v8::internal::OrderedHashTablev8::internal::OrderedHashMap,2::NumberOfElementsOffset+33350
3: 00007FF7B62A6ED6 v8::internal::OrderedHashTablev8::internal::OrderedHashMap,2::NumberOfElementsOffset+36886
4: 00007FF7B6B59F7E v8::Isolate::ReportExternalAllocationLimitReached+94
5: 00007FF7B6B3ED1D v8::SharedArrayBuffer::Externalize+781
6: 00007FF7B69FD02C v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1516
7: 00007FF7B64F6069 v8::internal::Isolate::FatalProcessOutOfHeapMemory+25
8: 00007FF7B6A26683 v8::internal::FactoryBasev8::internal::Factory::NewFixedDoubleArray+243
9: 00007FF7B68E6BC3 v8::debug::Script::GetIsolate+6003
10: 00007FF7B6771417 v8::internal::interpreter::JumpTableTargetOffsets::iterator::operator=+170215
11: 00007FF7B6BE2B5D v8::internal::SetupIsolateDelegate::SetupHeap+463949
12: 0000024A901CB154
PS C:\Users\PC-17\Desktop\trash\12.16_1\pagination>

1条回答
smile是对你的礼貌
2楼-- · 2020-12-22 12:47

因为你if判断写错了 兄弟 应该是

if(i==0)
查看更多
登录 后发表回答