56. Two Sum【easy】
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 zero-based.
Notice
You may assume that each input would have exactly one solution
numbers=[2, 7, 11, 15]
, target=9
return [0, 1]
Either of the following solutions are acceptable:
- O(n) Space, O(nlogn) Time
- O(n) Space, O(n) Time
题意
给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。
你需要实现的函数twoSum
需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是 0 到 n-1。
解法一:
- class Solution {
- public:
- /*
- * @param numbers: An array of Integer
- * @param target: target = numbers[index1] + numbers[index2]
- * @return: [index1 + 1, index2 + 1] (index1 < index2)
- */
- vector<int> twoSum(vector<int>& nums, int target) {
- // hash[i]表示nums中数值为i的下标
- unordered_map<int, int> hash;
- vector<int> result;
- // 一边循环每个数,一边加入hash表。
- for (int i = ; i < nums.size(); i++) {
- if (hash.find(target - nums[i]) != hash.end()) {
- // target - nums[i]的下标更小,放在前面
- result.push_back(hash[target - nums[i]]);
- result.push_back(i);
- return result;
- }
- hash[nums[i]] = i;
- }
- // 无解的情况
- result.push_back(-);
- result.push_back(-);
- return result;
- }
- };
使用万能的hash,参考@NineChapter 的代码
解法二:
- class Solution {
- public:
- /*
- * @param numbers: An array of Integer
- * @param target: target = numbers[index1] + numbers[index2]
- * @return: [index1 + 1, index2 + 1] (index1 < index2)
- */
- vector<int> twoSum(vector<int> &numbers, int target) {
- int len = numbers.size();
- int i, j;
- int flag = ;//flag作为找到答案后跳出的一个标记用变量
- for (i = ; i < len; ++i) {
- for (j = i + ; j < len; ++j) {
- if (numbers[i] + numbers[j] == target) {
- flag=;
- break;
- }
- }
- if (flag)
- break;
- }
- vector<int> ans;
- ans.push_back(i);
- ans.push_back(j);
- return ans;
- }
- };
暴力破解法,完全不推荐
56. Two Sum【easy】的更多相关文章
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 2. Trailing Zeros【easy】
2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
随机推荐
- FaceBook推出的Android图片加载库-Fresco
FaceBook推出的Android图片加载库-Fresco 原文链接:Introducing Fresco: A new image library for Android 译者 : ZhaoKai ...
- Spark Streaming中空batches处理的两种方法(转)
原文链接:Spark Streaming中空batches处理的两种方法 Spark Streaming是近实时(near real time)的小批处理系统.对给定的时间间隔(interval),S ...
- QT在windows下的安装与配置
先了解Qt: Qt一直以来,分为商业.开源两个版本,商业版本为用户提供了二级制的动态库,直接安装既可以使用,但是需要花钱购买license,而开源版本则遵守GPL协议,提供了源码,用户需要自行编译,才 ...
- jquery判断滚动条是否到底部
clientHeight:这个元素的高度,占用整个空间的高度,所以,如果一个div有滚动条,那个这个高度则是不包括滚动条没显示出来的下面部分的内容.而只是单纯的DIV的高度. offsetHeight ...
- zedboard--嵌入式网络摄像机(mjpg-streamer)的移植和搭建(二十二)
在zedboard上移植和搭建嵌入式网络摄像机mjpg-streamer.具体步骤如下: 来自:http://write.blog.csdn.net/postedit/13741451 1.安装lib ...
- LXD 2.0 系列(一):LXD 入门
LXD是提供了RESTAPI的LXC 容器管理器,主要是管理linux容器的第三方管理器.也许现在您还没有听说过,下面我们就来入门——介绍一下LXD 什么是 LXD ? 简单地说,LXD 就是一个提供 ...
- scala lambda 小括号与大括号
看akka源码的时候看到这样的一个用法: 作为接触scala两天半的我有些看不明白了.好一番搜索看到这样的答案: <scala雾中风景(2): 小括号与花括号> 下面的问题,表面上看是小括 ...
- Oracle spatial抽稀函数(SDO_UTIL.SIMPLIFY)
在使用Oracle spatial做空间查询和展示时,经常会遇到展示或者查询过慢,这时候我可以通过空间数据抽稀来优化查询展示效率. 在Oracle spatial中的抽稀函数为:SDO_UTIL.SI ...
- android.content.res.Resources$NotFoundException: String resource ID #0x0
仔细检查是不是在settext的时候设置进去的时int属性的值,所以android会认为这是在strings中的值,所以会拿着这个int值当做string的id值去找,结果当然是找不到的.
- .NET破解之繁星代码生成器
本教程只能用于学习研究,不可进行任何商业用途.如有使用,请购买正版,尊重他人劳动成果和知识产权! 对象:繁星代码生成器0.96 环境:Win7 x64 工具:exeinfoPE(查壳).de4dot ...