leetcode 日记 4sum java

整体思路同之前的一样,依然采取降低维度的方式进行

public List<List<Integer>> solution(int nums[], int target) {
        List<List<Integer>> result = new ArrayList<>();
        if((nums.length<4)||(nums==null))
        {
            return result;
        }
        Arrays.sort(nums);
        if ((4*nums[0]>target)||(4*nums[nums.length-1]<target))
        {
            return result;
        }//上面两个队特例的快速结束一定要加
        int N = nums.length;
        for (int i = 0; i < N; i++) {
            int[] temnum = new int[N - 1];
            System.arraycopy(nums, 0, temnum, 0, i);
            System.arraycopy(nums, i + 1, temnum, i, N - i - 1);
            result = threeSum(temnum, target - nums[i], nums[i], result);
        }
        return result;
    }

    public List<List<Integer>> threeSum(int[] nums, int target, int num,List<List<Integer>> result) {
        int sum;
        for (int i = 0; i < nums.length - 2; i++) {
            int start = i + 1, end = nums.length - 1;
            while (start < end) {
                sum = nums[i] + nums[start] + nums[end];
                if (sum < target) {
                    start++;
                    continue;
                }
                if (sum > target) {
                    end--;
                    continue;
                }
                //下面的部分为对每一结果内部进行进行排序,这样在去重时方便
                if (sum == target) {
                    List<Integer> list = new ArrayList<>();
                    if(num<nums[i])
                        list.add(num);
                    list.add(nums[i]);
                    if(num>=nums[i] && num<nums[start])
                        list.add(num);
                    list.add(nums[start]);
                    if ( num>=nums[start] &&num<nums[end])
                        list.add(num);
                    list.add(nums[end]);
                    if (num>=nums[end])
                        list.add(num);
                    start++;
                    end--;
                    if (result.contains(list))
                        continue;
                    result.add(list);
                }
            }
        }
        return result;
    }                

整体速度在leetcode上大概为50%

时间: 2024-08-07 14:37:38

leetcode 日记 4sum java的相关文章

leetcode 日记 3sumclosest java

整体思路为将threeSum将为twoSum即可 public int solution(int[] nums, int target) { if (nums.length == 3) { return nums[0] + nums[1] + nums[2]; } else { Arrays.sort(nums); int r = 10000;//此两处10000为省事而设,如果严谨应该大致找到其中的一个较大距离 int distance = 10000; for (int i = 0; i <

leetcode 18 4Sum JAVA

题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 注意: 答案中不可以包含重复的四元组. 解题思路 将nums数组重新排列,然后从i = 0开始遍历,分别取j = i + 1.left = j + 1和right = nums.length - 1,将nums[i].nums[j].nums[left]和nums[r

[LeetCode] 018. 4Sum (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 018.4Sum (Medium) 链接: 题目:https://oj.leetcode.com/problems/4sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给一个数列 S ,找出四个数 a,b,c,d 使得a + b + c + d = targ

[LeetCode] 454. 4Sum II 四数之和II

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the r

LeetCode 017 4Sum

[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending o

Leetcode: Wildcard Matching. java

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p

Leetcode: Text Justification. java

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad ex

leetcode 日记 162. Find Peak Element java python

根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号.并且要求时间复杂度为logn 分析:由于题目要求时间复杂度为logn,因此不能进行全部遍历.又因为只需要找到其中的一个峰值,那么,每次对半分,便可以达到logn的复杂度. 根据对半分的思路继续想,不难发现只要确定了中间位置的数是处在上升阶段还是下降阶段,就可以确定在某一侧必有一个峰值. 往复多次,即可找出两个点,其中一个一定处于某一个峰值上. java代码: 1 public int findPeakElement

Java for LeetCode 018 4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. 解题思路: 首先肯定是一次排序,如果是暴力枚举的话,肯定超时.因此,我们可以采用分治的思想,存储所有2个元素的和,然后采用2SUM的思路求解即可,