LeetCode1:Two Sum
题目:
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
解题思路:
这道题,很容易想到的就是先排序,在通过两前后指针,向中间靠拢。当两指针所指元素之和与target相等时,则可返回。这里指的注意的是,因为排序后各元素下标会打乱,所以应该构造一个结构体,包含要排序的元素值及最原始的下标。因为要用到排序,所以时间复杂度为O(nlogn)。
第二种思路就是,利用哈希表解决,因为哈希表查找时间复杂度为O(1),当处理一个元素时,判断target-cur是否在哈希表中,在这返回结果即可,这种解法的时间复杂度为O(n)。
实现代码:
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std; /**
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 */
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> resvec(2, 0);
if(numbers.empty())
return resvec;
unordered_map<int, int> imap;
int len = numbers.size();
// for(int i = 0; i < len; i++)
// imap[numbers[i]] = i+1;
for(int i = 0; i < len; i++)
{
//这里要注意,一开始,我直接利用数组元素初始化map,但会出现相同元素值覆盖的情况
if(imap.count(target - numbers[i]))
{
resvec[0] = imap[target - numbers[i]] + 1;
resvec[1] = i+1;
break;
}
imap[numbers[i]] = i; } return resvec; }
};
int main(void)
{
int arr[] = {2,7,11,15};
vector<int> numbers(arr, arr+4);
Solution solution;
vector<int> resvec = solution.twoSum(numbers, 9); vector<int>::iterator iter;
for(iter = resvec.begin(); iter != resvec.end(); ++iter)
cout<<*iter<<endl; return 0;
}
LeetCode1:Two Sum的更多相关文章
- LeetCode-1:Two Sum
[Problem:1-Two Sum] Given an array of integers, return indices of the two numbers such that they add ...
- LeetCode 题解(一):Two Sum
LeetCode : two sum 第一次写博客,算是熟悉这些编辑环境吧,本来是打算在csdn上用markdown写的,结果改了博客介绍就被关闭了,晕死...好了,话不多说,今天打算拿LeetCod ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- HDU1244:Max Sum Plus Plus Plus
题目链接:Max Sum Plus Plus Plus 题意:在n个数中取m段数使得这m段数之和最大,段与段之间不能重叠 分析:见代码 //dp[i][j]表示前i个数取了j段的最大值 //状态转移: ...
- SQL-W3School-函数:SQL SUM() 函数
ylbtech-SQL-W3School-函数:SQL SUM() 函数 1.返回顶部 1. SUM() 函数 SUM 函数返回数值列的总数(总额). SQL SUM() 语法 SELECT SUM( ...
- No.001:Two Sum
问题: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- HDU 1024:Max Sum Plus Plus(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...
- leetcode:Path Sum (路径之和) 【面试算法题】
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- Project Euler 83:Path sum: four ways 路径和:4个方向
Path sum: four ways NOTE: This problem is a significantly more challenging version of Problem 81. In ...
随机推荐
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 修改eclipse启动时eclipse使用的jre
eclipse在启动的时候,和环境变量中的jdk不兼容,可以单独制定eclipse运行的jre. 方法: 在eclipse的配置文件里增加-vm参数即可. 打开eclipse目录下的eclipse.i ...
- Oracle 一次生产分库,升级,迁移
今天完成了一个负载较高的中央数据库的分库操作, 并实现了oracle的滚动升级(10.2.0.1->10.2.0.4), 业务中断仅15分钟. 平台: RHEL AS 4 + Oracle 10 ...
- Wtl之奇技淫巧篇:一、SDI如何居中显示视图
Wtl的sdi应用,视图默认铺满框架的客户区.视图通常用modeless对话框,所有的界面元素都拥挤在左上角,这明显很丑陋.我们尝试让视图居中显示,保持原始大小,这是个很典型的问题,看似简单,诸多细节 ...
- 命令行下使用javah命令生成.h文件,出现“错误: 无法访问android.app.Activity 找不到android.app.Activity的类文件”的解决方法
在学习NDK中,当我在项目的bin/classes目录下使用javah命令生成头文件时,出现了“错误: 无法访问android.app.Activity 找不到android.app.Activity ...
- ThreadPoolExecutor
ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程 ...
- Apache2.4中开通HTTP基本认证
Apache2.4中开通HTTP基本认证,需要在Apache的配置文件中增加如下代码 WSGIPassAuthorization On 否则则无法认证
- Cannot open connection 解决办法
试了很多种网上找的办法,都不行,最后才发现是我的beans.xml中完全把下面 这一段代码给遗忘了,忘记写了.添加我就ok了. 我能说花了我近1个小时吗?坑姐哦! <bean class=&qu ...
- hybrid开发设计
hybrid方案背景 大部分业务都是在不停改变的,我们希望native不发布新版本就可以让线上用户使用新功能.我们要实现这样的方式,采用h5来实现就可以满足这一要求,准确说是native里提供一个装载 ...
- 分享一个Mongodb PHP封装类
<?php /** * Mongodb 基本操作API,支持基本类似关系统型数据库的操作接口 * * @version 1.0 * [说明] * * 1:该版本API实现了 Mongodb 中最 ...