zoukankan      html  css  js  c++  java
  • 三数之和、最接近目标值的三数之和

    1、三数之和

     

    2、最接近的三数之和

     

    package Algorithms;
    
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * @author : zhang
     * @version : 1.0
     * @date : Create in 2021/7/31
     * @description :
     */
    public class ThreeSum {
        public static void main(String[] args) {
            int[] nums = {-1, 0, 1, 2, -1, -4, 5};
            
            //测试三数之和等于0的解
            List<List<Integer>> lists = threeSum(nums);
            System.out.println(lists); //[[-4, -1, 5], [-1, -1, 2], [-1, 0, 1]]
    
            //测试三数之和最接近target的解
            int closer = threeSumCloser(nums, -7);
            System.out.println(closer);  //-6
        }
    
        //1、判断数组长度n,如果数组为null或者长度小于3,返回[]
        //2、对数组进行排序
        //3、遍历排序后数组
        //如果num[i]>0:因为已经排序后,所以后面不可能有三个数加和为0,直接返回结果
        //对于重复元素,跳过,避免出现重复解
        //令左指针L=i+1,右指针R=n-1,当L<R时,执行循环
        //当num[i]+nums[L]+nums[R]==0,执行循环,判断左界和右界是否和下一位置重复,去除重复解。并同时将L,R移到下一位置,寻找新的解
        //若和大于零,说明nums[R]太大,R左移
        //若和小于零,说明nums[R]太小,L右移
        public static List<List<Integer>> threeSum(int[] nums) {
            ArrayList<List<Integer>> lists = new ArrayList<>();
            //排序
            Arrays.sort(nums);
            //双指针
            int len = nums.length;
            for (int i = 0; i < len - 2; i++) {
                //如果num[i]>0:因为已经排序后,所以后面不可能有三个数加和为0,直接返回结果
                if (nums[i] > 0) return lists;
                //对于重复元素,跳过,避免出现重复解
                if (i > 1 && nums[i] == nums[i - 1]) continue;
    
                int curr = nums[i];
                int L = i + 1, R = len - 1;
                while (L < R) {
                    int tem = curr + nums[L] + nums[R];
                    if (tem == 0) {
                        List<Integer> list = new ArrayList<>();
                        list.add(curr);
                        list.add(nums[L]);
                        list.add(nums[R]);
                        lists.add(list);
                        //当num[i]+nums[L]+nums[R]==0,执行循环,判断左界和右界是否和下一位置重复,去除重复解
                        while (L < R && nums[L + 1] == nums[L]) L++;
                        while (L < R && nums[R - 1] == nums[R]) R--;
                        //并同时将L,R移到下一位置,寻找新的解
                        L++;
                        R--;
    
                    } else if (tem < 0) {//若和小于零,说明nums[R]太小,L右移
                        L++;
                    } else {//若和大于零,说明nums[R]太大,R左移
                        R--;
                    }
                }
            }
            return lists;
        }
    
        public static int threeSumCloser(int[] nums, int target) {
            Arrays.sort(nums);
            int result = nums[0] + nums[1] + nums[2];
            //i < nums.length - 2:避免设计指针的时候出现数组越界
            for (int i = 0; i < nums.length - 2; i++) {
                //对于重复元素,跳过,避免出现重复解
                if (i > 1 && nums[i] == nums[i - 1]) continue;
                int L = i + 1, R = nums.length - 1;
                while (L < R) {
                    int sum = nums[i] + nums[L] + nums[R];
                    if (Math.abs(sum - target) < Math.abs(result - target)) {
                        result = sum;
                        //判断左界和右界是否和下一位置重复,去除重复解
                        while (L < R && nums[L + 1] == nums[L]) L++;
                        while (L < R && nums[R - 1] == nums[R]) R--;
                R--;
                L++; }
    else if (sum > target) { R--; } else { L++; } } } return result; } }
  • 相关阅读:
    SIP 研究 API中文
    关于“ARC forbids explicit message send of release”错误
    Android获取屏幕尺寸和密度
    ScrollView 判断滑动到底部
    搭建JAVA版的webService
    MTK 开发
    android 2D动画实现
    android notification详解
    android 监听电话来去电
    SharePoint
  • 原文地址:https://www.cnblogs.com/zh-xiaoyuan/p/15084393.html
Copyright © 2011-2022 走看看