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.

  • 相关阅读:
    6 全局锁和表锁
    oracle ogg--ogg搭建过程中遇到的错误及处理
    5 深入浅出索引(下)
    4 深入浅出索引(上)
    oracle ogg 单实例双向-新增表,修改表结构(oracle-oracle
    oracle ogg 单实例双向复制搭建(oracle-oracle)--Oracle GoldenGate
    Iview 中 获取 Menu 导航菜单 选中的值
    idea中git分支的使用
    vue使用axios进行ajax请求
    web前端_Vue框架_设置浏览器上方的标题和图标
  • 原文地址:https://www.cnblogs.com/freeneng/p/3012155.html
Copyright © 2011-2022 走看看