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 ...
随机推荐
- linux 编译指定库、头文件的路径问题(转)
1. 为什么会出现undefined reference to 'xxxxx'错误? 首先这是链接错误,不是编译错误,也就是说如果只有这个错误,说明你的程序源码本身没有问题,是你用编译器编译时参数用得 ...
- <爬虫>相关的知识
1.概念.工具和HTTP 什么是爬虫 模拟客户端发送网络请求,获取响应,按照规则提取数据 爬虫的数据去哪了 展示到网页上(百度新闻,今日头条) 进行分析,从数据中寻找规律(指数网站:百度指数) 需要的 ...
- ArrayList底层代码解析笔记
通过底层代码可以学习到很多东西: public class ArrayList<E> extends AbstractList<E> implements List<E& ...
- hdu 3486
题意:n个人,每个人的价格a[ i ] ,求最少分几组,每组取一个人,多出来的人就不考虑,使得这取出人的价格大于k.(每组人数一样) 分析:每组取一个人,那这个人肯定是这组最大的,枚举多少组就可以 ...
- pytest_fixture--scope="session"
import pytest@pytest.fixture(scope="session")def login(): print("\n输入用户名密码登陆! configt ...
- 自学Oracle心得
基本术语: global name 全局数据库名 service name 服务名 SID 实例名 常用命令: 1. s ...
- mysql的各种锁简单总结
表总体上分为三种: 1.表锁 Myisam 开销小,并发低,加锁快,不会出现死锁问题:锁粒度大,发生锁冲突的概率最高. 2.行锁 innodb 开销大,并发高,加锁慢,会出现死锁问题:锁粒度小,发生 ...
- 随笔记录 shell脚本相关内容 2019-8-26
字符串截取: 假设变量为var=http://www.hao.com/123.htm1. # 号截取,删除左边字符,保留右边字符.echo ${var#*//}其中 var 是变量名,# 号是运算符, ...
- vue 学习五 深入了解components(父子组件之间的传值)
上一章记录了 如何在父组件中向子组件传值,但在实际应用中,往往子组件也要向父组件中传递数据,那么此时我们应该怎么办呢 1.在父组件内使用v-on监听子组件事件,并在子组件中使用$emit传递数据 // ...
- 【POJ3155】生活的艰辛Hard Life
题面 Description ADN公司内部共 n个员工,员工之间可能曾经因为小事有了过节,总是闹矛盾.若员工u和员工 v有矛盾,用边(u, v)表示,共 m个矛盾.最近,ADN公司内部越来越不团结, ...