问题描述:

Given an array of integers, 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

解决原理:

数组以升序排序

从数组S头、尾同时遍历数组,i为头端索引,j为尾端索引

若S[i]+S[j]=target,则返回对应索引

若S[i]+S[j]>target,则j--

否则i++

代码:

 class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
int i = ;
int j = numbers.size()-;
int sum;
vector<int> rs;
vector<int> tmp; for(int k = ; k < numbers.size(); k++)
tmp.push_back(numbers[k]); sort(tmp.begin(), tmp.end());
while(i < j){
sum = tmp[i] + tmp[j];
if(sum == target){
int m = ;
while(numbers[m] != tmp[i]) m++;
int n = ;
while(numbers[n] != tmp[j]) n++;
if(n == m){
n++;
while(numbers[n] != tmp[j]) n++;
} rs.push_back(m>n?n+:m+);
rs.push_back(m>n?m+:n+);
return rs;
}else if(sum < target){
i++;
}else{
j--;
}
}
return rs;
}
};

Q4: Two Sum的更多相关文章

  1. BZOJ 2653 middle 二分答案+可持久化线段树

    题目大意:有一个序列,包含多次询问.询问区间左右端点在规定区间里移动所得到的最大中位数的值. 考虑对于每个询问,如何得到最优区间?枚举显然是超时的,只能考虑二分. 中位数的定义是在一个序列中,比中位数 ...

  2. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  3. VB热点答疑(2016.5.11更新Q4、Q5)

    收录助教君在VB习题课上最常被问到的问题,每周更新,希望对大家有所帮助. Q1.如何让新的文本内容接在原来的内容后面/下一行显示? A1.例如,Label1.text原本的内容是"VB程序设 ...

  4. MDX Step by Step 读书笔记(七) - Performing Aggregation 聚合函数之 Sum, Aggregate, Avg

    开篇介绍 SSAS 分析服务中记录了大量的聚合值,这些聚合值在 Cube 中实际上指的就是度量值.一个给定的度量值可能聚合了来自事实表中上千上万甚至百万条数据,因此在设计阶段我们所能看到的度量实际上就 ...

  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. POJ 2739. Sum of Consecutive Prime Numbers

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

  9. BZOJ 3944 Sum

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

随机推荐

  1. jQuery中.parent和.parents的区别

    .parent(selector) 获得当前匹配元素集合中每个元素的父元素,由选择器筛选(可选). .parents(selector) 获得当前匹配元素集合中每个元素的祖先元素,由选择器筛选(可选) ...

  2. sqlserver中自定义函数+存储过程实现批量删除

    由于项目的需要,很多模块都要实现批量删除的功能.为了方便模块的调用,把批量删除方法写成自定义函数.直接上代码. 自定义函数: ALTER FUNCTION [dbo].[func_SplitById] ...

  3. Open vSwitch FAQ (三)

    Quality of Service (QoS) Q: How do I configure Quality of Service (QoS)? A: Suppose that you want to ...

  4. (实用篇)PHPExcel读取Excel文件的实现代码

    用PHPExcel读取Excel 2007 或者Excel2003文件,需要的朋友,可以参考下. 涉及知识点:  php对excel文件进行循环读取 php对字符进行ascii编码转化,将字符转为十进 ...

  5. Python是编译运行的

    虽然Python被说成是一种解释型语言,但是实际上,Python源程序要先经过编译,然后才能运行. 与Java语言类似,Python源程序编译之后得到的是字节码,交由Python虚拟机来运行. 关于这 ...

  6. ZOJ 3798--解题报告

    题目相关: 3798相关链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5330 Alice和Bob玩数字游戏, 这次Al ...

  7. HOG 梯度方向直方图简介(转载)

    一.基本HOG算法 HOG特征最早出现在SIFT算法中,由于其极强的图像特征描述能力,逐渐被人们熟知和广泛运用,其在目标检测方面表现尤为突出. HOG特征提取过程 步骤一:遍历图像每个像素点,以其为中 ...

  8. Mybatis学习 —— 包括所有 mybatis官网

    http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases

  9. activiti 中的签收与委托 操作

    原文:http://my.oschina.net/acitiviti/blog/350957 先看看activiti中关于TASK的数据库表: 其中有两个字段:OWNER_,ASSIGNEE_ 这两个 ...

  10. HMTL—表单

    <body> <form> 账号:<input type="text" value="123" /> <br /> ...