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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Locked ownable synchronizers(转)

    public class DeadLock { public static void main(final String[] args) throws Exception { final Object ...

  4. sum求和类题目

    今天看到这道题目:http://www.cnblogs.com/charlesblc/p/5930311.html 题目地址:https://leetcode.com/problems/split-a ...

  5. LeetCode - Two Sum

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

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

  7. Leetcode 笔记 112 - Path Sum

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

  8. ORA-28000: the account is locked 账户被锁

    这种情况可能是因为你输入错误的用户名密码达到10次,oracle给你锁住了. 解决方法: 首先 ~bash$ sqlplus /nolog SQL> conn sys/sys as sysdba ...

  9. POJ 2739. Sum of Consecutive Prime Numbers

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

随机推荐

  1. ORACLE添加作业

    --创建job declare job number; beginsys.dbms_job.submit(job,'prc_into_actiwager;',sysdate,'sysdate+30/( ...

  2. 增强iOS应用程序性能的提示和技巧(25个)

    转自 http://www.cocoachina.com/newbie/basic/2013/0522/6259.html 在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的 ...

  3. iOS 里面如何使用第三方应用程序打开自己的文件,调用wps其他应用打开当前应用里面的的ppt doc xls

    我们的自己的应用里面经常涉及的要打开ppt doc,这样的功能,以前总以为iOS沙盒封闭化,不可能实现,后来终于解决了 使用 UIDocumentInteractionController 来解决这一 ...

  4. 安卓开发service

    如果把Activity比喻为前台程序,那么service可以看做是一个后台程序.Service跟Activity一样也由Intent调用. 在工程里想要添加一个Service,先新建继承Service ...

  5. Jquery 点击空白处消失

    $(document).bind("click", function (e){ if ( $((e.target || e.srcElement)).closest("# ...

  6. cos-26上传

    在开发中常常需要上传文件,上传文件的方式有很多种,这里有一个cos实现的例子. 首先是要拷贝cos.jar包拷贝到WEB-INF/lib目录下,然后才进行编码. 创建一个可以进行自动重命名的Java文 ...

  7. laravel5.2学习资源

    研究laravel的过程中基本把国内关于laravel的资料给翻了一遍了: 整理了一些中文的资源如下: 一:教程系列 1:https://laravist.com/series/laravel-5-b ...

  8. Json串到json对象的转换

    JSON(JavaScript Object Notation) JS对象符号 是一种轻量级的数据交换格式 JavaScript eval()函数实现 (一) 标准格式 function JsonFo ...

  9. php基础知识【oop/mvc/orm/aop】

    OOP 面向对象编程是一种计算机编程架构.OOP 的一条基本原则是计算机程序是由单个能够起到子程序作用的单元或对象组合而成.OOP 达到了软件工程的三个主要目标:重用性.灵活性和扩展性.为了实现整体运 ...

  10. PuTTY + Xming 远程使用 Linux GUI

    from http://www.zw1840.com/blog/zw1840/2008/10/putty-xming-linux-gui.html 在家里的PC上用VMWare做了一个Oracle E ...