1. Two Sum【easy】
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) {
unordered_map<int, int> u_map;
vector<int> result;
for (int i = ; i < nums.size(); ++i)
{
if (u_map.find(target - nums[i]) != u_map.end())
{
result.push_back(u_map[target - nums[i]]);
result.push_back(i);
return result;
}
u_map.insert(make_pair(nums[i], i));
}
return result;
}
};
map搞一把
解法二:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
int n = (int)nums.size();
for (int i = ; i < n; i++) {
auto p = map.find(target - nums[i]);
if (p != map.end()) {
return {p->second, i};
}
map[nums[i]] = i;
}
}
};
比较简洁的写法
扩展见:
167. Two Sum II - Input array is sorted【easy】
170. Two Sum III - Data structure design【easy】
1. Two Sum【easy】的更多相关文章
- 56. Two Sum【easy】
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 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 ...
随机推荐
- 【莫队算法】【权值分块】bzoj3339 Rmq Problem
如题. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; #def ...
- 5.7(java学习笔记)Vector、Enumeration
一.Vector Vector类实现一个可扩展的数组对象.与数组一样,它包含可以使用整数索引访问. 它的基本操作方法add(int index, E element),get(int index),i ...
- Exercise01_02
public class Five{ public static void main(String[] args){ for(int i=0;i<5;i++){ System.out.print ...
- (转)关于Unity3D的编辑器崩溃时的线索定位
今天在Unity3D编辑器中进行功能测试的时候,编辑器突然崩溃了(就是整个窗口突然消失,进程直接结束)之后也没有任何错误报告信息提示.好吧,应该是偶现问题,我侥幸地想,我用的好歹也是正版啊,不应该总出 ...
- 将Java程序打jar包并运行
1)接着上篇博客继续说手动编译之后,将代码打成jar包,然后直接“java -jar lz.jar"运行不成功的问题.还是先上代码: 这个是Demo类: package org.lz.dem ...
- 每天一个linux命令13之curl发送http请求
一.get请求 curl "http://www.baidu.com" 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http:// ...
- 显示/隐藏Mac系统中所有的隐藏文件
显示: 在终端输入:defaults write com.apple.finder AppleShowAllFiles YES 隐藏: 在终端输入:defaults write com.apple.f ...
- Ubuntu 16.04通过APT源安装QUEM虚拟机调试Linux内核
安装: sudo apt-add-repository main sudo apt-get update sudo apt-get install qemu-kvm qemu virt-manager ...
- Fatal error: Maximum execution time of 30 seconds exceeded in
Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\Apache Software Found ...
- 利用CMD查看系统硬件信息
利用CMD查看系统硬件信息对于在windows下查看系统信息大家一定不陌生了,我现在说几个最常用的方法,对命令感兴趣的朋友看看,(给菜鸟看的,老手就不要笑话我了,大家都是从那个时候过来的,^_^).一 ...