zoukankan      html  css  js  c++  java
  • [算法]两数之和,三数之和

    1. 两数之和

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

    你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

    示例:

    给定 nums = [2, 7, 11, 15], target = 9
    
    因为 nums[0] + nums[1] = 2 + 7 = 9
    所以返回 [0, 1]

    LeetCode:https://leetcode-cn.com/problems/two-sum/description/

    思路:

    两层for循环时间复杂度是O(N)的想法大家应该都会,想想有没有时间复杂度更低的解法呢?

    答案就是利用hashmap,这样可以用hashmap的key记录差值,value记录下标,在hashmap中进行查找的时间复杂度是O(1),这样总的时间复杂度是O(N)。

    class Solution {
        public int[] twoSum(int[] a, int target) {
            int[] res = new int[2];
            HashMap<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < a.length; i++) {
                if(map.containsKey(a[i])){
                    res[0] = map.get(a[i]);
                    res[1] = i;
                }
                map.put(target - a[i], i);
            }
            return res;
        }
    }

    2. 三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

    注意:答案中不可以包含重复的三元组。

    例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
    
    满足要求的三元组集合为:
    [
      [-1, 0, 1],
      [-1, -1, 2]
    ]

    LeetCode:https://leetcode-cn.com/problems/3sum/description/

    思路:

    让时间复杂度尽可能小,先排序,时间复杂度可以是O(NlogN),然后用三个指针遍历一次即可,这样总的时间复杂度是O(NlogN)。

    当然也可以参考上道题,让hashmap进行记录,这样的话,需要记录两个值的组合,时间复杂度还是O(N^2)。

    public List<List<Integer>> threeSum(int[] nums) {
            if(nums == null){
                return null;
            }
            if(nums.length < 3){
                return new ArrayList<>();
            }
            
            Arrays.sort(nums);
            HashSet<List<Integer>> set = new HashSet<>();
            for (int i = 0; i < nums.length; i++) {
                int j = i + 1;
                int k = nums.length - 1;
                while(j < k){
                    if(nums[i] + nums[j] + nums[k] == 0){
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[i]);
                        list.add(nums[j]);
                        list.add(nums[k]);
                        set.add(list);
                        while(j < k && nums[j] == nums[j + 1]){
                            j++;
                        }
                        while(j < k && nums[k] == nums[k - 1]){
                            k--;
                        }
                        j++;
                        k--;
                    }else if(nums[i] + nums[j] + nums[k] < 0){
                        j++;
                    }else{
                        k--;
                    }
                }
            }
            return new ArrayList<>(set);
        }
  • 相关阅读:
    Redis 配置为 Service 系统服务
    java.lang.IllegalStateException: The platform metadata area could not be written
    SpringCloud Gateway做熔断降级+限流
    Mac mysql修改my.cnf不起作用排查
    MailHealthIndicator javax.mail.MessagingException: Could not connect to SMTP host: smtp.qiye.aliyun.com, port: 25, response: -1
    Spring Cloud Gateway跨域配置
    一、Rancher单机搭建
    SpringCloud快速搭建微服务
    在Mac下为GUI程序设定环境变量
    Spring cache 使用说明
  • 原文地址:https://www.cnblogs.com/DarrenChan/p/8871495.html
Copyright © 2011-2022 走看看