zoukankan      html  css  js  c++  java
  • [LeetCode]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

    题目大意是:给定一个数组array,和一个目标值target,找出数组中的两个数使得array[i] + array[j] = target,且i < j,返回i和j的下标,下标从1开始。

    解题思想:因为array中的数是非排序的,故要对array中的数做一个处理(当然你可以选择排序处理),这里使用map,建立array中元素和下标的关系(建立hash表, O(1)时间查找);然后遍历array,在map中查找target - array[i]是否存在;存在则记录其下标(下标要+1,因此数组下标从0开始)。

    code:

     1 class Solution {
     2 public:
     3     vector<int> twoSum(vector<int> &numbers, int target) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         map<int, int>h_map;
     7         int i;
     8         vector<int> indices;
     9         //build map
    10         for (i = 0; i < numbers.size(); ++i)
    11         {
    12             h_map.insert(pair<int, int>(numbers[i], i));
    13         }
    14         
    15         int index1 = 0;
    16         int index2 = 0;
    17 
    18         for (i = 0; i < numbers.size(); ++i)
    19         {
    20             //if h_map[target - numbers[i]] exist
    21             if (h_map.find(target - numbers[i]) != h_map.end())
    22             {
    23                 if (i == h_map[target - numbers[i]])
    24                     continue;
    25                 index1 = i;
    26                 index2 = h_map[target - numbers[i]];
    27                 break;
    28             }
    29         }
    30 
    31         if ( i < numbers.size())
    32         {    //pair exist
    33             if (index1 > index2)
    34             {    //swap
    35                 index1 ^= index2;
    36                 index2 ^= index1;
    37                 index1 ^= index2;
    38             }
    39             indices.push_back(index1 + 1);//non-zero based
    40             indices.push_back(index2 + 1);
    41         }
    42         return indices;
    43     }
    44 };


    test case:

    input                output 
    [5,75,25], 100   2, 3     
      
    [150,24,79,50,88,345,3], 200   1, 4
      
    [2,1,9,4,4,56,90,3], 8   4, 5 
      
    [230,863,916,585,981,404,316,785,88,12,70,435,384,778,887,755,740,337,86,92,325,422,815,650,920,125,277,336,221,847,168,23,677,61,400,136,874,363,394,199,863,997,794,587,124,321,212,957,764,173,314,422,927,783,930,282,306,506,44,926,691,568,68,730,933,737,531,180,414,751,28,546,60,371,493,370,527,387,43,541,13,457,328,227,652,365,430,803,59,858,538,427,583,368,375,173,809,896,370,789], 542    29, 46 
      
    [591,955,829,805,312,83,764,841,12,744,104,773,627,306,731,539,349,811,662,341,465,300,491,423,569,405,508,802,500,747,689,506,129,325,918,606,918,370,623,905,321,670,879,607,140,543,997,530,356,446,444,184,787,199,614,685,778,929,819,612,737,344,471,645,726], 789     11, 56 
      
    [722,600,905,54,47], 101     4, 5 
      
    [210,582,622,337,626,580,994,299,386,274,591,921,733,851,770,300,380,225,223,861,851,525,206,714,985,82,641,270,5,777,899,820,995,397,43,973,191,885,156,9,568,256,659,673,85,26,631,293,151,143,423], 35    40, 46 
      
    [286,461,830,216,539,44,989,749,340,51,505,178,50,305,341,292,415,40,239,950,404,965,29,972,536,922,700,501,730,430,630,293,557,542,598,795,28,344,128,461,368,683,903,744,430,648,290,135,437,336,152,698,570,3,827,901,796,682,391,693,161,145], 890    16, 35 

      
    [22,391,140,874,75,339,439,638,158,519,570,484,607,538,459,758,608,784,26,792,389,418,682,206,232,432,537,492,232,219,3,517,460,271,946,418,741,31,874,840,700,58,686,952,293,848,55,82,623,850,619,380,359,479,48,863,813,797,463,683,22,285,522,60,472,948,234,971,517,494,218,857,261,115,238,290,158,326,795,978,364,116,730,581,174,405,575,315,101,99], 163     55, 74 
      
    [678,227,764,37,956,982,118,212,177,597,519,968,866,121,771,343,561], 295      7, 9 
      

    自己写的mian函数

     1 int main()
     2 {    
     3     vector<int> numbers;
     4     numbers.push_back(2);
     5     numbers.push_back(7);
     6     numbers.push_back(11);
     7     numbers.push_back(15);
     8     Solution *ptr = new Solution();
     9     vector<int> indices = ptr->twoSum(numbers, 9);
    10     if (indices.size() > 0)
    11     {
    12         printf("index1 = %d, index2 = %d\n", indices[0], indices[1]);
    13     }
    14                     return 0;
    15 }

     

    不经历风雨,怎么见彩虹!
  • 相关阅读:
    MongoDB mongod.log "connection refused because too many open connections" 处理方法
    12C RAC 故障分析
    Linux(Redhat 7.6)安装PG(Postgresql 9.6.0)
    OGG异构平台安装部署 MySQL->Oracle
    Redhat 7安装Oracle 11.2.0.4 RAC 数据库软件中报错:Error in invoking target 'agent nmhs' of makefile
    Redhat 7.6安装11G RAC GI时遇到此类报错
    Oracle 19.3 RAC on Redhat 7.6 安装最佳实践
    RAC环境中某数据文件(非system表空间)创建在本地,不停机迁移到ASM磁盘中
    python 多线程
    go博客大全
  • 原文地址:https://www.cnblogs.com/ivorfeng/p/3072983.html
Copyright © 2011-2022 走看看