1 two sum

作者: yangminz | 来源:发表于2018-07-15 23:57 被阅读8次

title: Two Sum
tags:
- two-sum
- simple
- hash
- No.1
- integer


Problem

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 exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Solution

Brute-Force

Brute-Force runs in O(n^2):

class Solution {
    public int[] twoSum(int[] nums, int target) {
        // O(n^2)
        for (int i=0; i<nums.length-1; i++) {
            for (int j=i+1; j<nums.length; j++) {
                if (nums[i] + nums[j] == target)
                    return new int[] {i, j};
            }
        }
        
        throw new IllegalArgumentException("No solution");
    }
}

Twice Hash

Hash twice: First takes O(n) to build HashMap. Second to search the complement O(n) times in O(1):

import java.util.*;

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> val_loc = new HashMap<Integer, Integer>();
        
        // O(n)
        for (int i=0; i<nums.length; i++) {
            val_loc.put(nums[i], i);
        }
        
        // O(n)
        for (int i=0; i<nums.length; i++) {
            // O(1)
            Integer j = val_loc.get(target - nums[i]);
            
            if (j != null) {
                if (j != i) {
                    return (i < j) ? (new int[] {i, j}) : (new int[] {j, i});
                }
            }
        }
        
        throw new IllegalArgumentException("No solution");
    }
}

Once Hash

Search the existed elements in table while insert new pairs to it. Thus loop one time in O(n):

import java.util.*;

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> val_loc = new HashMap<Integer, Integer>();
        
        // O(n)
        for (int i=0; i<nums.length; i++) {
            // O(1)
            Integer j = val_loc.get(target - nums[i]);
            
            if (j != null) {
                if (j != i) {
                    return (i < j) ? (new int[] {i, j}) : (new int[] {j, i});
                }
            }
            
            val_loc.put(nums[i], i);
        }
                
        throw new IllegalArgumentException("No solution");
    }   
}

相关文章

  • LeetCode 1. Two Sum (Easy)

    LeetCode 1. Two Sum (Easy) LeetCode 1. Two Sum (Easy) 原题链...

  • 1. Two Sum

    1. Two Sum 题目:https://leetcode.com/problems/two-sum/ 难度: ...

  • leetcode hot100

    1. Two Sum[https://leetcode-cn.com/problems/two-sum/] 字典(...

  • leetCode #1

    #1. Two Sum

  • LeetCode之Swift

    1.Two Sum (Easy)

  • X Sums

    Two Sum It is not easy to write Two-Sum right for the fir...

  • 1 two sum

    title: Two Sumtags:- two-sum- simple- hash- No.1- integer...

  • 1、Two Sum

    题设 要点 Hash表,将双重循环的O(n^2)降到O(n) 排序+首尾指针,不断移动 该题其实就是一个很简单的搜...

  • 1 Two Sum

    Given an array of integers, return indices of the two num...

  • #1 Two Sum

    最近想刷leetcode 从头开始啦这题可以用暴力方法 O(n2)的时间复杂度和哈希表O(n)的复杂度,下面贴出h...

网友评论

      本文标题:1 two sum

      本文链接:https://www.haomeiwen.com/subject/hummpftx.html