1. The Sum of the

Source: Internet
Author: User

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

The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Less than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.

You may assume this each input would has exactly one solution.

Input:numbers={2, 7, one, A, target=9
Output:index1=1, index2=2

1. Index 1 must be smaller than index 2, which is key instead of value/where index1 must is less than Index2

2. Only the unique solution//may assume the eachinput would has exactly one solution.

1. At first, my idea was to sort the arrays and then pair the sorted array through a loop, so that the time complexity was O (n+n2)

public class Solution {public    int[] Twosum (int[] nums, int target) {        int tmp;        Boolean flag = false;        int numbers[] = new int[2];        for (int i = 0; i < nums.length-1; i++) {            tmp = nums[i];            if (Nums[i] >= nums[i+1]) {                nums[i] = nums[i+1];                NUMS[I+1] = tmp;            }        }        for (int i =0; i < nums.length &&!flag, i++) {for            (int J =0; J < Nums.length; J + +) {                if (i = = j) Con tinue;                else {                    if (Nums[i] + nums[j] = = target) {                        numbers[0] = i+1;                        NUMBERS[1] = j+1;                        Flag = true;                        Break        ;        }}}} return numbers;}    }


2. However, the problem of doing so causes the index of the final output to be the index of the sorted array, and if each subscript of the original array is marked, the time complexity is significantly greater than the tolerable range

3. In fact, through the HashMap ContainsKey method can be very brief to solve the problem

public class Solution {    //The idea of solving a problem is to find another addend by looking for the result of the target minus one addend in the HashMap    //For example, the array [3,2,4], target = 6    // First run, map no content, not satisfied with the judgment, add 3 to HashMap    //second run, target-num[1] = 4, do not meet the judgment, will 2 days off to HashMap    //Third run, target-num[2] = 2, Satisfies the judgment, returns [Addend in Map, current Addend]//index1 must is less than index2    //Will be wonderful in 1. by target-nums[i] This condition, the sum composition of the search and index1 must b e less than INDEX2 conditions    //          2. Using the ContainsKey on the pass means that the key is used as value to use    //          3. Java can be int[]{by return new ...} To return the array directly, which also embodies the principle that everything in Java is an object public    int[] Twosum (int[] nums, int target) {        Map<integer, integer> Map = New Hashmap<integer, integer> ();        for (int i = 0; i < nums.length; i++) {            if (Map.containskey (Target-nums[i])) return new Int[]{map.get (target-num S[i]), i+1};            Map.put (Nums[i], i+1);        }        return new int[]{0,0};}    }


1. The Sum of the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.