Algo: Two Sum
类似的题目可以用HashTable的思想解决。
1、Two Sum
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.
https://leetcode.com/problems/two-sum/description/
http://www.cnblogs.com/grandyang/p/4130379.html
https://blog.csdn.net/gatieme/article/details/50596965
https://segmentfault.com/a/1190000006697526
#include <vector>
#include <unordered_map> std::vector<int> twoSum(std::vector<int>& nums, int target)
{
int size = (int)nums.size(); std::unordered_map<int, int> mp;
std::vector<int> ans; for(int i = ; i < size; ++i)
{
if(mp.count(target - nums[i]))
{
ans.push_back(mp[target - nums[i]]);
ans.push_back(i); break;
} mp[nums[i]] = i;
} return ans;
}
2、Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
https://www.cnblogs.com/grandyang/p/5185815.html
#include <vector> std::vector<int> twoSum(std::vector<int>& numbers, int target)
{
int l = ;
int r = (int)numbers.size() - ; while (l < r)
{
int sum = numbers[l] + numbers[r];
if (sum == target)
{
return {l + , r + };
}
else if (sum < target)
{
++l;
}
else
{
--r;
}
} return {};
}
3、
Algo: Two Sum的更多相关文章
- 308. Range Sum Query 2D - Mutable
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- 307. Range Sum Query - Mutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II
1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
随机推荐
- Feign 系列(02)Why Feign
Feign 系列(02)Why Feign [toc] 1. 什么是 Feign Feign 的英文表意为"假装,伪装,变形", 是一个 Http 请求调用的轻量级框架,可以以 J ...
- js小项目:显示与输入的内容相关的
1,添加键盘抬起事件 2,获取文本框的内容,是否与数组中的内容匹配 3,创建元素 <!DOCTYPE html> <html lang="en"> < ...
- Couleur(启发式 + 主席树)(终于补坑了)
ZOJ Problem Set - 4053 Couleur Time Limit: 6 Seconds Memory Limit: 131072 KB DreamGrid has an a ...
- python之保留有限的历史记录(collections.deque)
1.deque(maxlen=N)创建一个固定长度的队列,当有新的记录加入而队列已经满时,会自动移除老的记录. from collections import deque q = deque(maxl ...
- Java利用MD5WithRSA签名及DESede加密
前言:近期公司做数据加密及签名,整理如下: 一.数字签名. 是只有信息的发送者才能产生的别人无法伪造的一段数字串,具有不可抵赖性,可验证信息完整性的一种手段. 签名不可伪造:其他人因为没有对应的私钥, ...
- 继续搞我的linux
小程序研发已经告一段落,还是继续我的Linux研究.上次因为捣鼓那个fastab,结果吧虚拟机搞崩溃了.好吧,这次老子来装正式机,从机房拉来了一台破烂货,联想的老式服务器,开工吧. 用UltraISO ...
- 利用ffmpeg进行视频软解播放
前段时间,公司的一个项目需要一个rtsp的播放库,原本打算直接用vlc播放的,但我觉得vlc太庞大了,很多功能没必要,还不如用ffmpeg+d3d简单的实现一个库,因此就有了今天讲的这个东西.一个解码 ...
- vue axios----基于 Promise 的 HTTP 请求
vue axiosvue2.0之axios接口請求管理功能特性axios API開始使用get請求post请求多个请求并发拦截器移除一个拦截器:自定义的 axios 实例添加拦截器:vue2.0之ax ...
- Python 3.3 IDLE 删除键出现空格卡顿
Python 3.3 IDLE 删除文字时会出现"□",造成删除键卡顿 原因:使用的是百度输入法,由于输入法的编码方式不同,对IDLE造成了不兼容 换成了搜狗输入法问题解决.... ...
- 关于mybaitis
mybatis启动流程 1.首先来看看最简单的mybatis项目启动过程 public static void mybatisTest() throws IOException { String re ...