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 ...
随机推荐
- 深度学习教程Deep Learning Tutorials
Deep Learning Tutorials Deep Learning is a new area of Machine Learning research, which has been int ...
- 关于TagHelper的那些事情——自定义TagHelper(格式化输出、依赖注入使用)
自定义TagHelper的最后一步就是在Process方法或ProcessAsync方法中添加展现代码.熟悉WebControl开发的朋友都知道Render方法,在这个方法中会添加展现的Html元素和 ...
- java获取系统进程号
public static final int jvmPid() { try { RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean( ...
- 【云计算】Docker容器不能修改hosts文件怎么解决?
参考资料: http://bbs.csdn.net/topics/390871429 http://tieba.baidu.com/p/4295556808 http://stackoverflow. ...
- scala for循环
scala for循环功能强大啊,for条件可以写各种表达式 通过一个demo来看一下,这个是一个讲yield关键字的demo:<Scala中的yield> object YieldDem ...
- 智能化的命令行工具-betty
本文首先公布在 CSDN的文章编辑器实在太烂了,建议大家用Cmd Markdown.它不但支持markdown.还支持VIM模式. 智能化的命令行工具-betty betty tool command ...
- (C++)已知String类的定义,实现其函数体
CString类的定义如下: class CMyString{ public: CMyString(const char* pData=NULL); CMyString(const CMyString ...
- android 线程安全
android ui 不是线程安全的,所以不能在子线程里更新ui,必须到主线程里更新
- daemon与服务(service)及重启服务的方法
简单地说,系统为了某些功能必须要提供一些服务(不论是系统本身还是网络方面),这个服务就称为service.而实现这个service的程序我们就称它为daemon.实现某个服务是需要一个daemon在后 ...
- 简单测试Demo:如何用Java压缩文件夹和文件
一.直接贴出测试代码 package com.jason.zip; import java.io.File; import java.io.FileInputStream; import java.i ...