TwoSum

/*
     * Given an array of integers, return indices of the two numbers such that they add up 
     * to a specific target.
     * You may assume that each input would have exacly one solution, and you may not use
     * the same element twice.
     * Example:
     *      Give nums = [2, 7, 11, 15], target = 9,
     *      Because nums[0] + nums[1] = 2 + 7 = 9,
     *      return [0,1]
     */
  1. 暴力
    暴力算法时间复杂度O(n²),空间复杂度O(1)
public class TwoSum {
    
    public static void main(String[] args) {
        int[] nums = new int[] {2, 7, 11, 15};
        int target = 9;
        int[] result = twoSum(nums, target);
        for(int i : result) {
            System.out.print(i + "\t");
        }
    }
    
    public static int[] twoSum(int[] nums, int target) {
        ArrayList<Integer> resultList = new ArrayList<Integer>();
        for(int i = 0; i < nums.length; i++) {
            for(int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target) {
                    resultList.add(i);
                    resultList.add(j);
                }
            }
        }
        int[] result = new int[resultList.size()];
        for(int i = 0; i < resultList.size(); i++) {
            result[i] = resultList.get(i);
        }
        return result;
    }
}
  1. 两次遍历 HashMap
    时间复杂度:O(n),
    我们把包含有 n 个元素的列表遍历两次。由于哈希表将查找时间缩短到 O(1) ,所以时间复杂度为 O(n)。

空间复杂度:O(n),
所需的额外空间取决于哈希表中存储的元素数量,该表中存储了 n 个元素。

public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for(int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if(map.containsKey(complement) && map.get(complement) != i) {
                return new int[] {i, map.get(complement)};
            }
        }
        throw new IllegalArgumentException("No two sum solution.");
    }
  1. 一次遍历 HashMap
    进行迭代并将元素插入到表中的同时,回过头来检查表中是否已经存在当前元素所对应的目标元素。如果它存在,那找到了对应解,并立即将其返回
    时间复杂度:O(n),
    我们只遍历了包含有 n 个元素的列表一次。在表中进行的每次查找只花费 O(1) 的时间。
    空间复杂度:O(n),
    所需的额外空间取决于哈希表中存储的元素数量,该表最多需要存储 n 个元素。
public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if(map.containsKey(complement)) {
                return new int[] {map.get(complement), i};
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution.");
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 两数之和 问题描述: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。你可以假设每个输入只对应一种答案...
    lijianfex阅读 2,944评论 0 0
  • 小小程序员的我准备没事刷刷leetcode的题,顺便把当时的想法记录~ 英文原题 ...
    萝卜青菜瓜阅读 13,277评论 1 1
  • 前言 平生不识TwoSum,刷尽LeetCode也枉然。欢迎开启LeetCode刷题的旅程,这将是一段漫长而又艰辛...
    奔跑吧技术人阅读 2,990评论 0 3
  • 题目大意: 找到数组中两个元素相加等于指定数的所有组合 情况一:给定数组中不含重复元素,且均为正整数 思路: 使用...
    Ferrari1001阅读 1,852评论 0 0
  • 题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回...
    93张先生阅读 1,033评论 0 0