博客
关于我
L15,L16三数之和
阅读量:215 次
发布时间:2019-02-28

本文共 2365 字,大约阅读时间需要 7 分钟。

三数之和问题分为两个部分:判断是否存在三个数之和为零,以及找到和目标最接近的三数之和。以下是详细的解决方案和代码实现。

三数之和为零的判断

方法思路

  • 排序数组:首先对数组进行排序。排序可以帮助我们利用双指针技巧,有效地减少重复计算。
  • 双指针遍历:选择第一个数后,使用两个指针分别从数组的后端和中间开始遍历。通过调整这两个指针的位置,可以找到满足条件的三元组。
  • 记录结果:记录所有满足条件的三元组,确保不重复。
  • 代码实现

    import java.util.ArrayList;import java.util.List;public class Solution {    public List
    > threeSum(int[] nums) { List
    > result = new ArrayList<>(); if (nums == null || nums.length < 3) { return result; } Arrays.sort(nums); List
    > tempResult = new ArrayList<>(); for (int i = 0; i < nums.length - 2; i++) { List
    current = new ArrayList<>(); current.add(nums[i]); int j = i + 1; int k = nums.length - 1; while (j < k) { int sum = nums[i] + nums[j] + nums[k]; if (sum == 0) { current.add(nums[j]); current.add(nums[k]); tempResult.add(current); // 跳过重复的元素 while (j < k && nums[j] == nums[j + 1]) { j++; } while (k > j && nums[k] == nums[k - 1]) { k--; } j++; k--; } else if (sum > 0) { k--; } else { j++; } } } // 去除重复的三元组 for (List
    list : tempResult) { result.add(list); } return result; }}

    最接近目标值的三数之和

    方法思路

  • 排序数组:同样对数组进行排序,以便利用双指针技巧。
  • 遍历数组:使用双指针遍历数组,计算当前三元组的和与目标值的差距。
  • 跟踪最接近值:在遍历过程中,记录最接近目标值的三元组和。
  • 代码实现

    public class Solution {    public int threeSumClosest(int[] nums, int target) {        if (nums == null || nums.length < 3) {            return -1;        }        Arrays.sort(nums);        int closest = nums[0] + nums[1] + nums[2];        int len = nums.length;        for (int i = 0; i < len - 2; i++) {            int j = i + 1;            int k = len - 1;            while (j < k) {                int sum = nums[i] + nums[j] + nums[k];                int diff = Math.abs(sum - target);                int currentDiff = Math.abs(closest - target);                if (diff < currentDiff) {                    closest = sum;                } else if (diff == currentDiff) {                    closest = Math.max(closest, sum);                }                if (sum < target) {                    j++;                } else if (sum > target) {                    k--;                } else {                    return sum;                }            }        }        return closest;    }}

    总结

    通过对数组排序和双指针遍历的方法,我们能够高效地解决这两个问题。首先,排序数组帮助我们快速定位可能的三元组,双指针则有效地减少了搜索空间。同时,我们记录了所有满足条件的三元组,确保结果的准确性和唯一性。在处理最接近目标值的问题时,我们在遍历过程中动态地更新最接近值,保证了算法的高效性。

    转载地址:http://bcvp.baihongyu.com/

    你可能感兴趣的文章
    Oracle计划将ZGC项目提交给OpenJDK
    查看>>
    Oracle闪回技术(Flashback)
    查看>>
    oracle零碎要点---ip地址问题,服务问题,系统默认密码问题
    查看>>
    oracle零碎要点---oracle em的web访问地址忘了
    查看>>
    Oracle零碎要点---多表联合查询,收集数据库基本资料
    查看>>
    Oracle静默安装
    查看>>
    Oracle面试题:Oracle中truncate和delete的区别
    查看>>
    ThreadLocal线程内部存储类
    查看>>
    thinkphp 常用SQL执行语句总结
    查看>>
    Oracle:ORA-00911: 无效字符
    查看>>
    Text-to-Image with Diffusion models的巅峰之作:深入解读 DALL·E 2
    查看>>
    TCP基本入门-简单认识一下什么是TCP
    查看>>
    tableviewcell 中使用autolayout自适应高度
    查看>>
    Orcale表被锁
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>