[Locked] Two Sum
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.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
代码:
vector<int> index(vector<int> numbers, int target) {
vector<int> vi;
int begin = , end = int(numbers.size() - ), sum = numbers[begin] + numbers[end];
while(sum != target) {
sum > target ? end-- : begin++;
sum = numbers[begin] + numbers[end];
}
vi.push_back(begin + );
vi.push_back(end + );
return vi;
}
Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add
and find
.
add
- Add the number to an internal data structure.find
- Find if there exists any pair of numbers which sum is equal to the value.
For example,
add(1); add(3); add(5);
find(4) -> true
find(7) -> false
代码:
class Solution {
private:
unordered_multiset<int> hash; public:
void add(int num) {
hash.insert(num);
return;
}
bool find(int target) {
for(int num : hash) {
if(target - num == num && hash.count(num) >= )
return true;
else if(hash.find(target - num) != hash.end())
return true;
}
return false;
}
};
[Locked] Two Sum的更多相关文章
- [Locked] Range Sum Query 2D - Mutable
Range Sum Query 2D - Mutable Given a 2D matrix matrix, find the sum of the elements inside the recta ...
- [Locked] Maximum Size Subarray Sum Equals k
Example 1: Given nums = [1, -1, 5, -2, 3], k = 3,return 4. (because the subarray [1, -1, 5, -2] sums ...
- Locked ownable synchronizers(转)
public class DeadLock { public static void main(final String[] args) throws Exception { final Object ...
- sum求和类题目
今天看到这道题目:http://www.cnblogs.com/charlesblc/p/5930311.html 题目地址:https://leetcode.com/problems/split-a ...
- 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 ...
- ORA-28000: the account is locked 账户被锁
这种情况可能是因为你输入错误的用户名密码达到10次,oracle给你锁住了. 解决方法: 首先 ~bash$ sqlplus /nolog SQL> conn sys/sys as sysdba ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
随机推荐
- Android图表引擎AChartEngine之折线图使用
最近在帮老师做一个课题,其中app端需要显示折线图以便直观地看数据波动,上网查了些资料后发现了这款图标引擎,另外感谢李坤老师的博客,帮助很大. 废话不多说,下面写代码. 一.AChartEngine是 ...
- SQL中使用的一些函数问题
abs()取绝对值ceil()上取整floor()下取整initcap()使串中的所有单词的首字母变为大写substr()取子串 这些函数都是oracle的sql内置函数.
- sublime text 2 笔记
sublime text 2 ,是代码程序员最佳编辑器,不是之一.其快捷优雅的操作风格,和便利的快捷键,是程序员码农的不二选择. 网上下载sublime text 2,支持文件拖放,文件夹拖放.3.0 ...
- PV、UV、IP的区别
网站推广需要一个网站访问统计工具,常用的统计工具有百度统计.51la.量子恒道统计等.网站访问量常用的指标为PV.UV.IP.那么什么是PV.UV和IP,PV.UV.IP的区别是什么? --首先来看看 ...
- 【BZOJ2752】【线段树】高速公路
Description Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这条高速公路上设立了许多收费站. Y901高速公路是一条由N-1段路以及N个 ...
- 神秘链接__proto__是什么鬼
_proto_实际上是某个实例对象的隐藏属性,而prototype是其构造器函数(或者说‘类’)的原型属性; function Mine() {} var hi = new Function(), ...
- dedecms自定义表单提交成功如何返回当前页面
在plus/diy.php找到showmsg($bkmsg, $goto);改成showmsg($bkmsg, -1);
- access_token的获取2
概述 access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存. access_token的存储至少要保留512个字符空间.acces ...
- 在php中使用Memcache
在上一篇博客中我写到了memcache的安装和管理,在这一篇博客中我将写到的是在php中如何使用memcache. 一.安装memcache扩展 首先我们通过phpinfo()函数查看一下我们当前的p ...
- TatukGIS - GisDefs - CreateMSJET 函数
函数名称 CreateMSJET 所在单元 GisDefs 函数原型 function CreateMSJET(const _path: String): String; ...