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 ...
随机推荐
- linux学习(一)开始
第一关 用u盘安装ubuntu, 大部份工作制作的安装U盘会失败,使用Win32DiskImager就行了,这个工具需要手动填写完整iso路径. 第二个问题 装完后发现乱码,连英文都乱码,不知道原因, ...
- Git错误
$ rm -rf .git $ git config --global core.autocrlf false $git init $git add . ---------------------- ...
- Spring知识点总结(一)
1. 框架概述 所谓的框架其实就是程序的架子,在这个程序的架子中,搭建起程序的基本的骨架,针对程序的通用问题给出了便捷的解决方案,可以使开发人员 基于框架快速开发具体的应用程序. ...
- 为什么需要Vlan ? Vlan实现原理 ? 不同Vlan的通信 ?
好文章!!良心推荐!!! 原文链接 https://blog.csdn.net/cwm_meng_home/article/details/49762807
- 2小时学会spring boot 以及spring boot进阶之web进阶(已完成)
1:更换Maven默认中心仓库的方法 <mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirr ...
- Vue--- VueX组件间通信链接(共有方法放入了扩展目录store里面) 1.2
Vuex结构图再仔细看 Vuex原理: 就是 把共有属性放入到一个公共的地方,进行使用 多组件共享状态, 之前操作方式,由父组件传递到各个子组件. 当路由等加入后,会变得复杂. 引入viewx 解决 ...
- 百度 suggestion 学习demo
其他说明文字就不用写那么多了,代码很简单,相信各位道友都能看懂,看不懂的琢磨一下就可以看懂啦.贴代码!拷贝到自己的电脑中运行文件即可,不需要服务器. <!DOCTYPE html> < ...
- DOM查询
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Linux系统运维基础测试题
1 Linux运维基础测试题(第一关) 通过这段时间学习Linux基础命令,为了检测自己对Linux基础命令掌握的情况,从网上整理13到测试题,并将其整理出来供大家参考学习. 1.1 习题 ...
- vue渲染自定义json数据
<template> <div class="wrap"> <div class="main"> <div class ...