首页 新闻 搜索 专区 学院

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

0
[已解决问题] 解决于 2020-12-22 12:56

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>

scofield1906的主页 scofield1906 | 初学一级 | 园豆:165
提问于:2020-12-17 19:55
< >
分享
最佳答案
1

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

if(i==0)
奖励园豆:5
winds_随风 | 初学一级 |园豆:151 | 2020-12-18 16:17

还真是耶!多加一个等于号就好了。谢谢

scofield1906 | 园豆:165 (初学一级) | 2020-12-22 12:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册