# Leetcode 001-Sum

Source: Internet
Author: User

Title Description: Both Sum

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 Les S 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

This problem can have the following 3 kinds of solutions:

1. Two-layer cycle, brute force solution. The complexity is O (N2), the test does not pass;

2. The hash table, known as the first number, calculates the second number, and the existence is found. The degree of complexity is O (n);

3. Sort first, then add force to left and right. The sort is O (n logn), the left and right force is O (n), the synthesis is O (n logn).

1. Two-layer cycle, brute force solution

`classSolution { Public: Vector<int> Twosum (vector<int> &numbers,inttarget) {                intI, J, sum; Vector<int>results;  for(i=0; I<numbers.size ()-1; i++){             for(j=i+1; J<numbers.size (); J + +){                if(Numbers[i] + numbers[j] = =target) {Results.push_back (i+1); Results.push_back (J+1);  Break; }            }        }                returnresults; }};`

2. Hash table

`classSolution { Public: Vector<int> Twosum (vector<int> &numbers,inttarget) {Unordered_map<int,int>mapping; Vector<int>result;  for(inti =0; I < numbers.size (); i++) {Mapping[numbers[i]]=i; }                 for(inti =0; I < numbers.size (); i++){            intGap = target-Numbers[i]; if(Mapping.find (GAP)! = Mapping.end ())//mapping the corresponding primary key is not found, return to Map.end ()                if(I! = Mapping[gap]) {//The same number cannot be added two timesResult.push_back (i +1); Result.push_back (Mapping[gap]+1);  Break; }        }                returnresult; }};`

3. Sort first, then force left and right

`structnode{intNum//numbers[]    intPos//Index};BOOLCMP (Node A, Node B) {returnA.num <B.num;}classSolution { Public: Vector<int> Twosum (vector<int> &numbers,inttarget) {                intI, J; Vector<int>result; Vector<Node>Array;  for(i =0; I < numbers.size (); i++) {Node temp; Temp.num=Numbers[i]; Temp.pos=i;        Array.push_back (temp); }                //Sortsort (Array.begin (), Array.end (), CMP); //left and right to force         for(i =0, j = numbers.size ()-1; I! =J;) {                        intsum = Array[i].num +Array[j].num; if(Sum = =target) {                if(Array[i].pos <Array[j].pos) {Result.push_back (Array[i].pos+1); Result.push_back (Array[j].pos+1); }Else{result.push_back (Array[j].pos+1); Result.push_back (Array[i].pos+1); }                 Break; }                        Else if(Sum < target) i++; Else if(Sum > Target) j--; }                returnresult; }};`

Summary of Knowledge points:

1. Sort function

The sort function has three parameters:
(1) The first one is the starting address of the array to sort.
(2) The second is the ending address (the last address to sort)
(3) The third parameter is the method of sorting, can be from large to small but also small to large, but also can not write the third parameter, the default sorting method is from small to large sort.

The sort function uses templates:
Sort (start,end, sort method)

Leetcode 001-Sum

Related Keywords:

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.