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 ...
随机推荐
- 《CUDA并行程序设计:GPU编程指南》
<CUDA并行程序设计:GPU编程指南> 基本信息 原书名:CUDA Programming:A Developer’s Guide to Parallel Computing with ...
- Out of memory: Kill process 内存不足
服务直接被 killed,感觉特别奇怪.代码肯定是没有问题的,但为什么放到服务器上就出错了呢. 部署时报错如下: Failed to add the deployment content to the ...
- VC++多线程--进程间通信
1.邮槽 邮槽是windows系统提供的一种单向通信的机制,邮槽能传输的数据非常小,一般在400k左右. 创建邮槽 HANDLE CreateMailslot( LPCTSTR lpName, //指 ...
- java学习笔记7--抽象类与抽象方法
接着前面的学习: java学习笔记6--类的继承.Object类 java学习笔记5--类的方法 java学习笔记4--类与对象的基本概念(2) java学习笔记3--类与对象的基本概念(1) jav ...
- Android -- Activity的销毁和重建
两种销毁 第一种是正常的销毁,比如用户按下Back按钮或者是activity自己调用了finish()方法: 另一种是由于activity处于stopped状态,并且它长期未被使用,或者前台的acti ...
- Thinkphp学习笔记-controller与view绑定
$this->display(); 通过上面的代码则可以输出controller所对应的view
- php内置的http server, 类似于nodejs里面和golang里面的
原文:https://www.sitepoint.com/taking-advantage-of-phps-built-in-server/ ----------------------------- ...
- Android安装包相关知识汇总 (编译过程图给力)
转自: https://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ==&mid=208008519&idx=1&sn=278b7793699 ...
- (剑指Offer)面试题13:在O(1)时间内删除链表结点
题目: 在给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间内删除该结点.链表结点与函数的定义如下: struct ListNode{ int val; ListNode* next; } ...
- 【PAT】1028. List Sorting (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1028 题目描述: Excel can sort records according to an ...