LeetCode - 1. Two Sum(8ms)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
map<int, int> m;
for(int i=; i<n; i++) {
int temp = target - nums[i];
map<int, int>::iterator it = m.find(temp);
if(it != m.end()) {
return vector<int> {it->second, i};
}
m[nums[i]] = i;
}
}
};
LeetCode - 1. Two Sum(8ms)的更多相关文章
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- Eclipse插件的卸载和安装
Eclipse 卸载插件: 右下角会有卸载进度 卸载完后 然后需要重启 Eclipse安装插件 选择本地下载好的插件 点击 Ok 插件下载地址:https://jaist.dl.sourceforge ...
- 数据库——MySQL——单表查询
单表查询语法: SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 关键字的执行 ...
- Linux_vsftpd服务配置
首先安装Linux 企业版第一张光盘中的vsftpd-2.0.1-5.i386.rpm#rpm –ivh /media/cdrom/RedHat/RPMS/vsftpd-3.0.1-5.i386.rp ...
- iOS之一个iOS开发人员完整的学习路线
iOS开发能力 掌握(最好是精通)OC语言和runtime各种细节(读过相关的clang源码和runtime源码为佳).精通基本的framework(Foundation,UIKit等,平时干活用得最 ...
- c# 开发可替换的通用序列化库
开篇继续吹牛.... 其实没有什么可吹的了,哈哈哈哈哈 主要是写一个通用库,既可以直接用,又方便替换,我的序列化都是采用第三方的哈. 我不上完整代码了,只是给大家讲讲过程. 1.写一个序列化的类,我是 ...
- BZOJ1086: [SCOI2005]王室联邦(贪心,分块?)
Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 2610 Solved: 1584[Submit][Status] ...
- 最长公共子序列Lcs (51Nod - 1006)
20180604 11:28 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,ab ...
- 分布式日志系统ELK搭建
ELK:Elasticsearch Logstash Kibana Elasticsearch:是基于JSON的分布式搜索和分析引擎,专为实现水平扩展.高可用和管理便捷性而设计 Logstash:是 ...
- 关于SSM项目注解事务不回滚的问题
<!--扫描service包(包含子包)下所有使用注解的类型--> <context:component-scan base-package="com.song.ssm.s ...
- 解决百度编辑器ueditor插入视频网址保存不了问题
问题:如下图,在百度编辑器中插入视频,视频网址可以识别,但是提交内容后视频却显示不了. 解决:这个问题主要是编辑器中会过滤一些html标签,所以可以给标签添加白名单. 修改 ueditor.confi ...