zoukankan      html  css  js  c++  java
  • LeeCode-Two Sum

    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

     1 class Solution {
     2 public:
     3     vector<int> twoSum(vector<int> &numbers, int target) {
     4         vector<int>::iterator it,jt;
     5         int index1,index2;
     6         vector<int> OutputIndex;
     7         vector<int>::size_type number_size=numbers.size();
     8         int Judge=0;
     9         for(index1=1,it=numbers.begin()-1;it<numbers.end()-1;index1++,it++)
    10         {
    11             for(index2=1+index1,jt=numbers.begin();jt<numbers.end()-1;index2++,jt++)
    12                 {
    13                     if((*jt+*it)==target)
    14                     {
    15                         Judge=1;
    16                         break;
    17                     }
    18                 }
    19                 
    20                 if(Judge==1)
    21                     break;
    22                 
    23         }
    24         OutputIndex.push_back(index1);
    25         OutputIndex.push_back(index2);
    26         
    27         return OutputIndex;
    28     }
    29 };
  • 相关阅读:
    洛谷P3674 小清新人渣的本愿
    洛谷P3709 大爷的字符串
    CF747F Igor and Interesting Numbers
    洛谷P1494 小Z的袜子
    洛谷P3396 哈希冲突
    分块⑨题
    洛谷P4219 大融合
    bzoj2959 长跑
    洛谷P2486 染色
    洛谷P4382 劈配
  • 原文地址:https://www.cnblogs.com/vpoet/p/4660550.html
Copyright © 2011-2022 走看看