• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

Four Sum

我的解法

介绍

太好了!虽然并不是每次,但是在测试代码的时候,居然出现了内存使用率超过了100.00%!虽然并非每次都是,但是这也证明了代码的可用性。

思路

可以参考三和法和二和法,不过多赘述https://www.cnblogs.com/mapodoufu/p/16215421.html :号和https://www.cnblogs.com/mapodoufu/p/16216178.html

代码

/*

* @Author: fox

* @ date : 2022-05-02 18:43:16

* @ LastEditors: fox

* @ lastedittime : 2022-05-02 22:16:43

* @描述: https://leetcode.com/problems/4sum/

*/

/**

* @描述:运行时: 91.50%内存使用量: 100.00%

* @param {number[]} nums

* @param {number}目标

* @return {number[][]}

*/

const fourSum=(nums,target)={

const RES=[];//返回结果

const len=nums .长度

nums.sort((a,b)=a - b) //升序排序

对于(设I=0;I len-3;i ) {

如果(我0!(努姆斯^努姆斯[i - 1])继续//去重

对于(设j=I ^ 1;j len-2;j ) {

如果(j i 1!(努姆斯[j] ^努姆斯[j - 1])继续//去重

对于(设左=j ^ 1,右=len-1;左右;) {

const sum=numsnums[j]nums

nums

;//求和

如果(总目标){

左;

继续;

}

如果(总目标){

右-;

继续;

}

res.push([nums,nums[j],nums

,nums

])

而(!(努姆斯[左]努姆斯[左1])左右)左;//去重处理

而(!(nums[右]^ nums[右- 1])左右)右-;//去重处理

左;

右-;

}

}

}

返回资源

};

让记录总数

nums=[1,0,-1,0,-2,2]

console.log(fourSum(nums,0)) //[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

nums=[2,2,2,2]

console.log(fourSum(nums,8)) //[[2,2,2,2]]

nums=[0,0,0,0]

console.log(fourSum(nums,0)) //[0,0,0,0]

Link to comment
Share on other sites