问题描述:

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. sql 字段重复值,in,like

  2. android死机问题

    一般在平时工作中,基本上很多代码可以在eclipse+ndk进行调试,但如果需要用到具体的硬件设备,如媒体播放设备无法模拟的情况下,只能上硬件(盒子或手机)上进行调试.此时唯一的调试手段就是logca ...

  3. cout 计算顺序问题

    cout输出流的执行顺序   下面是IBM的一道笔试题 #include <iostream> using namespace std; int fun( ) { cout << ...

  4. 配置PhoneGap 到iOS

    下载 phonegap安装phonegap之前需要NodeJS环境,下载NodeJS并安装.安装环境的目的是为了使用phonegap命令行. 3. 安装phonegap使用命令    $phonega ...

  5. IOS 作业项目(4)步步完成 画图 程序(上)

    先上流程图

  6. Making the Grade_滚动数组&&dp

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

  7. Unity3D ShaderLab 立方体图的反射遮罩

    Unity3D ShaderLab 立方体图的反射遮罩 上一篇,简单的介绍了立方体图的反射,那么我们能不能使用一张纹理对其进行指定遮罩呢?这样美工可以更好的控制图像的效果. 我们接着使用上一篇的sha ...

  8. Unity3D ShaderLab法线贴图

    Unity3D ShaderLab法线贴图 说到法线贴图,应该算是我们最常使用的一种增强视觉效果的贴图.将法线贴图的各个像素点座位模型的法线,这样我们的光照可以模拟出高分辨率的效果, 同时也保持较低的 ...

  9. I.MX6 KEY_ROW4 can't as GPIO pin

    /********************************************************************** * I.MX6 KEY_ROW4 can't as GP ...

  10. PHP startsWith and endsWith

    function startsWith($haystack, $needle) { // search backwards starting from haystack length characte ...