博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Two Sum
阅读量:4556 次
发布时间:2019-06-08

本文共 4325 字,大约阅读时间需要 14 分钟。

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
twoSum(vector
&numbers, int target) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 map
h_map; 7 int i; 8 vector
indices; 9 //build map10 for (i = 0; i < numbers.size(); ++i)11 {12 h_map.insert(pair
(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]] exist21 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 exist33 if (index1 > index2)34 { //swap35 index1 ^= index2;36 index2 ^= index1;37 index1 ^= index2;38 }39 indices.push_back(index1 + 1);//non-zero based40 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
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
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 }

 

 

转载于:https://www.cnblogs.com/ivorfeng/archive/2013/05/11/3072983.html

你可能感兴趣的文章
PHP wamp server问题
查看>>
Spring Data Redis学习
查看>>
js闭包理解案例-解决for循环为元素注册事件的问题
查看>>
2015.04.23,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 33
查看>>
Spring+SpringMVC+JDBC实现登录
查看>>
生与死之间
查看>>
NEFU 109
查看>>
HDU 5435
查看>>
git从已有分支拉新分支开发
查看>>
滚动条隐藏兼容写法
查看>>
SQL2005查询所有表的大小
查看>>
Shell 正则表达式
查看>>
Docker run命令参数整理
查看>>
qt-opencv配置mingw编译器
查看>>
CSS之Medial Queries的另一用法:实现IE hack的方法
查看>>
linux-CentOS6.4下安装oracle11g详解
查看>>
实力为王 八年DBA经验谈
查看>>
2-sat 问题 【例题 Flags(2-sat+线段树优化建图)】
查看>>
ext3.2 右击动态添加node的treepanel
查看>>
Database links
查看>>