zoukankan      html  css  js  c++  java
  • [LintCode] Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number.

    The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are NOT zero-based.

    You may assume that each input would have exactly one solution

    Example

    numbers=[2, 7, 11, 15], target=9

    return [1, 2]

    Challenge 

    Either of the following solutions are acceptable:

    • O(n) Space, O(nlogn) Time
    • O(n) Space, O(n) Time

    Solution 1. O(n) runtime, O(n) space using hash map, two pass

     1 public class Solution {
     2     public int[] twoSum(int[] numbers, int target) {
     3         int[] result = new int[2];
     4         if(numbers == null || numbers.length <= 1)
     5         {
     6             return result;
     7         }
     8         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
     9         for(int i = 0; i < numbers.length; i++)
    10         {
    11             map.put(numbers[i], i);
    12         }
    13         for(int i = 0; i < numbers.length; i++)
    14         {
    15             if(map.containsKey(target - numbers[i]))
    16             {
    17                 result[0] = i + 1;
    18                 result[1] = map.get(target - numbers[i]) + 1;
    19                 break;
    20             }
    21         }
    22         return result;
    23     }
    24 }

    Solution 2. O(n) runtime, O(n) space using hash map, one pass

     1 public class Solution {
     2     public int[] twoSum(int[] numbers, int target) {
     3         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();       
     4         for(int i = 0; i < numbers.length; i++)
     5         {
     6             if(map.get(numbers[i]) != null)
     7             {
     8                 int[] result = {map.get(numbers[i]) + 1, i + 1};
     9                 return result;
    10             }
    11             map.put(target - numbers[i], i);
    12         }
    13         int[] result = {};
    14         return result;
    15     }
    16 }

    Solution 3.  O(n*logn) runtime, O(n) space using sort and two pointers 

     1 class ArrayEntry
     2 {
     3     protected int val;
     4     protected int index;
     5     public ArrayEntry(int val, int index)
     6     {
     7         this.val = val;
     8         this.index = index;
     9     }
    10 }
    11 public class Solution {
    12     public int[] twoSum(int[] numbers, int target) {
    13         int[] result = new int[2];
    14         if(numbers == null || numbers.length <= 1)
    15         {
    16             return result;
    17         }
    18         ArrayEntry[] entries = new ArrayEntry[numbers.length];
    19         for(int i = 0; i < numbers.length; i++)
    20         {
    21             entries[i] = new ArrayEntry(numbers[i], i);
    22         }
    23         Arrays.sort(entries, new Comparator<ArrayEntry>()
    24             {
    25                 public int compare(ArrayEntry a1, ArrayEntry a2)
    26                 {
    27                     return Integer.compare(a1.val, a2.val);
    28                 }
    29             }
    30         );
    31         int i = 0, j = entries.length - 1;
    32         while(i < j)
    33         {
    34             int sum = entries[i].val + entries[j].val;
    35             if(sum == target)
    36             {
    37                 if(entries[i].index < entries[j].index)
    38                 {
    39                     result[0] = entries[i].index + 1;
    40                     result[1] = entries[j].index + 1;
    41                 }
    42                 else
    43                 {
    44                     result[0] = entries[j].index + 1;
    45                     result[1] = entries[i].index + 1;
    46                 }
    47                 break;
    48             }
    49             else if(sum < target)
    50             {
    51                 i++;
    52             }
    53             else
    54             {
    55                 j--;
    56             }
    57         }
    58         return result;
    59     }
    60 }

    Related Problems

    Two Sum - Input array is sorted 

    Two Sum - Difference equals to target 

    Two Sum - Less than or equal to target

    Two Sum - Data structure design

    Two Sum - Unique pairs

    Two Sum - Closest to target

    Two Sum - Greater than target 

    Triangle Count

    3Sum Closest

    4Sum

    3Sum

  • 相关阅读:
    Ubuntu 16.04 安装 .NET Core[转]
    SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试 [T]
    npm package
    RN 文件上传下载
    RN css整理
    react nginx配置
    macOS安装homebrew报错 errno 54 & port 443:Connection refused
    shopify插件开发oauth报400的解决办法
    Mac下更新Ruby版本
    Failed to connect to raw.githubusercontent.com port 443 解决方案
  • 原文地址:https://www.cnblogs.com/lz87/p/7203577.html
Copyright © 2011-2022 走看看