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 }

     

    不经历风雨,怎么见彩虹!
  • 相关阅读:
    浅析Scrapy框架运行的基本流程
    排序和搜索
    设计模式:桥接模式及代码示例、桥接模式在jdbc中的体现、注意事项
    设计模式:适配器模式(类适配器、对象适配器、接口适配器)
    设计模式:建造者模式及在jdk中的体现,建造者模式和工厂模式区别
    java的线程、创建线程的 3 种方式、静态代理模式、Lambda表达式简化线程
    设计模式:原型模式介绍 && 原型模式的深拷贝问题
    设计模式:工厂设计模式介绍及3种写法(简单工厂、工厂方法、抽象工厂)
    设计模式:单例模式介绍及8种写法(饿汉式、懒汉式、Double-Check、静态内部类、枚举)
    设计模式七大原则及代码示例
  • 原文地址:https://www.cnblogs.com/ivorfeng/p/3072983.html
Copyright © 2011-2022 走看看