zoukankan      html  css  js  c++  java
  • [Leetcode 13] 1 Two Sum

    Problem:

    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.

    Input: numbers={2, 7, 11, 15}, target=9
    Output: index1=1, index2=2

    Analysis:

    Naive iterating through the whole array will not work due to time limit. To solve n-sum problem, we need to sort the array and use two pointers start from head and tail to try the possible combination. But the problem here requires the original-index of the two elements. What we can do is using another aux array that has a copy of the original array. Then if two elements' sum equals the target, we use search algorithm in the aux to get the original index. Note that there's may be multiple same valued elements, after we get first element, we set this value to MIN_VALUE to avoid repeat. The element repeating also forbids us to use the hash method to solve ths problem -- which is more efficient. For the convenience of implementing, we use ArrayList here to serve as the aux array.

    Code:

    View Code
     1 public class Solution {
     2     public int[] twoSum(int[] numbers, int target) {
     3         // Start typing your Java solution below
     4         // DO NOT write main() function
     5         int[] res = new int[2];
     6         ArrayList<Integer> aux = new ArrayList<Integer>(numbers.length);
     7         
     8         for (int i=0; i<numbers.length; i++)
     9             aux.add(numbers[i]);
    10             
    11         Arrays.sort(numbers);
    12         int i=0, j=numbers.length-1;
    13         while ( i < j ) {
    14             int sum = numbers[i] + numbers[j];
    15             
    16             if (sum == target) {
    17                 int idx1 = aux.indexOf(numbers[i]);
    18                 aux.set(idx1, Integer.MIN_VALUE);
    19                 int idx2 = aux.indexOf(numbers[j]);
    20             
    21                 if (idx1 < idx2) {
    22                     res[0] = idx1+1;
    23                     res[1] = idx2+1;
    24                 } else {
    25                     res[0] = idx2+1;
    26                     res[1] = idx1+1;
    27                 }
    28                 
    29                 break;
    30             } else if (sum > target) {
    31                 j--;
    32             } else {
    33                 i++;
    34             }
    35         }
    36         
    37         return res;
    38     }
    39 }

    Attention:

    The start-end search method for 2-sum requires sort the array and thus change the corresponding relationship between element and index.

    HashMap<Elem, Index> won't work here due to the redudant value.

  • 相关阅读:
    git刚初始化项目的操作
    git在不同平台windows、linux、mac 上换行符的问题
    HTTP请求报文和HTTP响应报文
    记一次挂马清除经历:处理一个利用thinkphp5远程代码执行漏洞挖矿的木马
    Shell 一键安装命令
    Linux下ThinkPHP网站目录权限设置
    Linux fdisk普通分区扩容
    cachecloud安装部署
    python
    【转】【Python】Python网络编程
  • 原文地址:https://www.cnblogs.com/freeneng/p/3012155.html
Copyright © 2011-2022 走看看