Leetcode 1 two Sum (dichotomy)

Source: Internet
Author: User

Title Source: https://leetcode.com/problems/two-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

Problem Solving Ideas:

Title: Give an array of numbers and targets and target. Requires that two numbers in the array be found to be equal to the subscript corresponding to target. (Subscript is not 0!) )

/* First time do leetcode unfamiliar. Always write int main (), always to WA. (Obviously the local test has been passed). Then you understand the code requirements. */

Method: The array is sorted in ascending or descending order (sorted in ascending order below), and the binary method is used to search.

It is divided into three situations:

1 if (num[left].x+num[right].x==target) 2 Else if (num[left].x+num[right].x>target) 3 Else if (Num[left].x+num[right].x<target)

The corresponding operation:

            if(num[left].x+num[right].x==target) {L=num[num[left].sort_id].id; R=num[num[right].sort_id].id;  Break; }//the movement of left and right below depends on whether the sort is in ascending or descending orderElse if(num[left].x+num[right].x>target) { Right=right-1; }            Else if(num[left].x+num[right].x<target) { Left=left+1; }

Give the code:

#include <bits/stdc++.h>#defineMAX 10010using namespacestd;structnode{intx; intID; intsort_id;};BOOLCMP (Node A,node b) {returna.x<b.x;} Node Num[max];intMain () {intN,target,l,r;  while(~SCANF ("%d",&N)) { for(intI=0; i<n;i++) {scanf ("%d",&num[i].x); Num[i].id=i+1; } scanf ("%d",&target); Sort (Num,num+n,cmp);  for(intI=0; i<n;i++) {num[i].sort_id=i; }        intleft=0, right=n-1;  while(left<Right ) {            if(num[left].x+num[right].x==target) {L=num[num[left].sort_id].id; R=num[num[right].sort_id].id;  Break; }            Else if(num[left].x+num[right].x>target) { Right=right-1; }            Else{ Left=left+1; }} printf ("%d%d\n", L,r); }}

Submit Code:

1 classSolution {2  Public:3vector<int> Twosum (vector<int>& Nums,inttarget) {4vector<int>result;5         intn =nums.size ();6         if(N <2)7             returnresult;8vector<int> Original =nums;9 sort (Nums.begin (), Nums.end ());Ten         intleft=0, right=n-1; One         intI, J, smaller, bigger; A          while(Left <Right ) -         { -             if(Nums[left]+nums[right] = =target) the             { -                  for(i=0; i<n; i++) -                 { -                     if(Nums[left] = =Original[i]) +                         { -Result.push_back (i+1); +                              Break; A                         } at                 } -                  for(j=n-1; j>=0; j--) -                 { -                     if(Nums[right] = =Original[j]) -                     { -Result.push_back (j+1); in                          Break; -                     } to                 } +                 if(result[0] < result[1]) -                 { thesmaller = result[0]; *Bigger = result[1]; $                 }Panax Notoginseng                 Else -                 { thesmaller = result[1]; +Bigger = result[0]; A                 } theresult[0] =smaller; +result[1] =bigger; -                 returnresult; $             } $             Else if(Nums[left]+nums[right] <target) -left = left +1; -             Else theright = right-1; -         }Wuyi         returnresult; the     } -};

Leetcode 1 two Sum (dichotomy)

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.