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 ...
随机推荐
- android RecyclerView (三):ItemAnimator 详解
本文继上篇 ItemDecoration 之后,是深入理解 RecyclerView 系列的第二篇,关注于 ItemAnimator,主要是分析 RecyclerView Animators 这个库的 ...
- Python学习 —— 阶段综合练习三
Python学习 —— 阶段综合练习三 综合之前文件与文件夹操作的学习,做以下实例练习:(建议先不要看代码,自己先试着写:代码仅供参考,有多种实现方法) 1. 目录文件遍历(二层目录结构) 1). ...
- nmap速查表v1.0
基本语法: #nmap [扫描方式] [命令选项] {目标} 扫描目标格式: IPv4 地址: 192.168.1.1IPv6 地址:AABB:CCDD::FF%eth0主机名:www.targe ...
- 自动签到升级版(JS实现的每日定时任务)
公司规定每日签到两次:日子太安逸了,有时候中午居然会忘记签到…… 于是,笔者寻思写一个自动签到的脚本:每天指定两个签到时段,每次打开页面,先检测当前是否为签到时段,如果在签到时段,则检查cookie中 ...
- java 内存泄漏和内存溢出
参考:https://blog.csdn.net/eff666/article/details/52784724 1.内存溢出 内存溢出:OOM(OutOfMemoryError)异常,即程序需要内 ...
- React 同构思想
作者:yangchunwen React比较吸引我的地方在于其客户端-服务端同构特性,服务端-客户端可复用组件,本文来简单介绍下这一架构思想. 出于篇幅原因,本文不会介绍React基础,所以,如果你还 ...
- Sqlserver 使用CTE如何按子查询排序?
需求:查出最近有更改的客户信息(按最后更改时间排序,来自SystemLog表LogDateTime字段) 说明: Customer:客户信息表SystemLog:系统日志表,记录所有表信息的增,删,改 ...
- [Angular] Angular Elements Intro
Make sure install the latest Angular v6 with Angular CLI. Checkout ght Github for the code. 1. Creat ...
- Windows Server 2012怎样部署Domain Controller
用过Windows Server2008 系统的运维师们,可能习惯于用dcpromo的方式部署Domain Controller,可是在WindowsServer2012操作系统已经把这样的部署方式取 ...
- MongoDB 基础命令 (MongoDB Shell)
1.我们 mongodb 安装成功后,用上一篇的方法启动 mongodb服务 然后使用 mongodb shell 来做数据库的增删改查 2.创建数据库 语法: use 数据库名称 案例: > ...