• [算法]两数之和,三数之和


    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);
        }
  • 相关阅读:
    iOS 3DTouch 的开发套路大全
    启动图后面 添加有倒计时的广告图
    激光推送报错:APNs is not available,please check your provisioning profile and certification 和 设置别名问题 app not registed, give up set tag:
    iOS 微信和支付宝关于回调处理
    Android 开机自启动应用
    android快捷键
    如何让Eclipse的智能提示像VS一样霸气
    Win7下Eclipse中文字体太小
    Android的SharedPreferences实用技巧
    Java基础知识总结
  • 原文地址:https://www.cnblogs.com/DarrenChan/p/8871495.html
Copyright © 2020-2023  润新知