[Leetcode] 1.Two Sum

Source: Internet
Author: User

"title"

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, target=9
Output: index1=1, index2=2

"Analysis"

Two digits similar to the sum of a sword's offer and S

"Code"

/********************************** Date: 2015-01-15* sjf0115* title: 1.Two sum* Website: https://oj.leetcode.com/problems/two -sum/* Result: ac* Source: leetcode* Blog: **********************************/#include <iostream> #include <algorithm&gt ; #include <vector>using namespace Std;class solution {public:vector<int> twosum (vector<int> &nu        mbers, int target) {vector<int> result;        vector<int> num = numbers;        int count = Numbers.size ();        if (Numbers.empty ()) {return result;        }//if//sorting sort (Numbers.begin (), Numbers.end ());            Binary lookup variants for (int i = 0,j = Count-1;i < J;) {int sum = Numbers[i] + numbers[j];            Locate the target if (sum = = target) {return FindIndex (Num,numbers[i],numbers[j]);            }//current and greater than target, need to be smaller some else if (sum > Target) {j--;         }//current and less than target, need to grow some else{       i++;    }}//for return result; }private://Looking for subscript vector<int> findindex (vector<int> &numbers,int num1,int num2) {int count =        Numbers.size ();        vector<int> result;        int index1,index2;        BOOL Flag1 = False,flag2 = false;                 for (int i = 0;i < Count;++i) {if (Flag1 = = False && Numbers[i] = = NUM1) {index1 = i+1;            Flag1 = true;                } else if (Flag2 = = False && Numbers[i] = = num2) {index2 = i+1;            Flag2 = true;            }}//for//Exchange makes INDEX1 < INDEX2 if (Index1 > Index2) {int tmp = INDEX1;            Index1 = Index2;        INDEX2 = tmp;        }//if Result.push_back (INDEX1);        Result.push_back (INDEX2);    return result;    }};int Main () {solution solution;    Vector<int> num;    Num.push_back (0);    Num.push_back (4); Num.push_back (3);    Num.push_back (0);    Num.push_back (15);    int target = 0;    Find vector<int> VEC = solution.twosum (num,target);    Output cout<< "index1->" <<vec[0]<< "index2->" <<vec[1]<<endl; return 0;}


[Leetcode] 1.Two Sum

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.