类似的题目可以用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的更多相关文章

  1. 308. Range Sum Query 2D - Mutable

    题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  2. 307. Range Sum Query - Mutable

    题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  3. 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 ...

  4. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  5. 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 ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  9. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

随机推荐

  1. csv 基本操作, 报错解决(UnicodeEncodeError: 'utf-8' codec can't encode characters in position 232-233: surrogates not allowed)

    最常用的一种方法,利用pandas包 import pandas as pd #任意的多组列表 a = [1,2,3] b = [4,5,6] #字典中的key值即为csv中列名 dataframe ...

  2. linux 创建用户并限制其访问目录

    1.创建用户及访问目录 useradd test1 -d /usr/share/webapps/test -M 设置密码 passwd  test1 将访问目录权限全部赋予用户 chown -R te ...

  3. 使用shell脚本查看文件类型

    显示文件类型 #如查看 /etc 目录 [root@localhost ~]# sh test.sh /etc /etc/ [目录文件] #如查看 /etc 目录下所有文件 [root@localho ...

  4. 6个步骤,全方位掌握 Kafka

    毋庸置疑,目前 Apache Kafka 是整个消息引擎领域的执牛耳者,也是大数据生态圈中颇为重量级的一员. 从最早诞生于 LinkedIn 的"分布式消息系统",到现在集成了分发 ...

  5. python学习10—迭代器、三元表达式与生成器

    python学习10—迭代器.三元表达式与生成器 1. 迭代器协议 定义:对象必须提供一个next方法,执行该方法或者返回迭代中的下一项,或者返回一个StopIteration异常,以终止迭代(只能往 ...

  6. 操作bin目录下的文件

    string dir = AppDomain.CurrentDomain.BaseDirectory + "Video"; if (!System.IO.Directory.Exi ...

  7. sql ibatis

    <!-- 写入单位下当前参保人员 --> <insert id="insertTempCaz043" parameterClass="map" ...

  8. 安装Storm的基本过程

  9. union 或者 union all 与 order by 的联合使用

    首先清楚:多个select 语句 union 时不是简单的将查询结果拼接起来 而是将sql拼接起来编译(做为一个sql语句),然后去执行. 注: union 连接的语句中只会出现一个order by ...

  10. C++11 auto 与 右值

    auto: auto T = xxx; // 产生一个变量,自动推导变量类型.  存在变量拷贝的消耗.auto& T = xxx; // 产生一个变量的引用,自动推导变量类型.减少拷贝的消耗. ...