18. 四数之和

if(numsk > target && nums0 >= 0){

break;

}

java 复制代码
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        int len = nums.length;
        Arrays.sort(nums);
        for(int k = 0;k < len;k++){
            //三数之和中的>0就直接跳过是有条件的
           

            if(k > 0 && nums[k] == nums[k-1]){
                continue;
            }
            for(int i = k+1;i<len;i++){
                if(i>k+1 && nums[i] == nums[i-1]){
                    continue;
                }
                int left = i+1;
                int right = len - 1;
                while(left < right){
                    long sum = nums[k] + nums[i] + nums[left] + nums[right];
                    if(sum > target){
                        right--;
                    }else if(sum < target){
                        left++;
                    }else{
                        res.add(Arrays.asList(nums[k],nums[i],nums[left],nums[right]));
                        while(right > left && nums[right] == nums[right - 1]){
                            right--;
                        }
                        while(right > left && nums[left] == nums[left + 1]){
                            left++;
                        }
                        left++;
                        right--;
                    }
                }
            }
        }
        return res;
    }
}
相关推荐
tudousisi2223 分钟前
01背包8.21
数据结构·算法
ZJU_统一阿萨姆20 分钟前
【算子开发】Reduction算子完全指南
人工智能·算法·语言模型
青 春 记 忆28 分钟前
LeetCode 155. 最小栈|Python 解法详解
开发语言·python·leetcode
鹿角片ljp9 小时前
LeetCode 46. 全排列|吃透回溯
算法·leetcode·职场和发展
鼎艺创新科技9 小时前
不依赖 UE/Unity:我们如何从零搭建一套国产三维 GIS 渲染引擎
人工智能·算法·unity·游戏引擎·三维电子沙盘
石头猫灯10 小时前
WordPress wp2shell 漏洞链完整拆解流程
网络·数据结构·安全·web安全
.道阻且长.12 小时前
11.LeetCode算法习题讲解--滑动窗口--将x减到0的最小操作数
算法·leetcode·职场和发展
wenyq712 小时前
LeetCode 2460. Apply Operations to an Array
算法·leetcode
.格子衫.13 小时前
032动态规划之区间DP——算法备赛
算法·动态规划
青 春 记 忆13 小时前
LeetCode 142. 环形链表 II|Python 解法详解
python·leetcode·链表