Q4: Two Sum
问题描述:
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的更多相关文章
- BZOJ 2653 middle 二分答案+可持久化线段树
题目大意:有一个序列,包含多次询问.询问区间左右端点在规定区间里移动所得到的最大中位数的值. 考虑对于每个询问,如何得到最优区间?枚举显然是超时的,只能考虑二分. 中位数的定义是在一个序列中,比中位数 ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- VB热点答疑(2016.5.11更新Q4、Q5)
收录助教君在VB习题课上最常被问到的问题,每周更新,希望对大家有所帮助. Q1.如何让新的文本内容接在原来的内容后面/下一行显示? A1.例如,Label1.text原本的内容是"VB程序设 ...
- MDX Step by Step 读书笔记(七) - Performing Aggregation 聚合函数之 Sum, Aggregate, Avg
开篇介绍 SSAS 分析服务中记录了大量的聚合值,这些聚合值在 Cube 中实际上指的就是度量值.一个给定的度量值可能聚合了来自事实表中上千上万甚至百万条数据,因此在设计阶段我们所能看到的度量实际上就 ...
- 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 ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
随机推荐
- 关于offer选择
6月1日收到移动调剂到昭通移动的电话,当时第一反应就是拒绝,后来参考了很久,犹豫了很久,答应了hr:答应了就有点后悔了:各种挑刺为难Hr;6月2日上午回绝hr: 问:陈姐,我有件重要的事忘记问了,在昭 ...
- H5+app前端后台ajax交互总结
流应用开发 1.前端是HBuilder 编写的html页面,UI控件用MUI: 2.后台用Eclipse开发的Servlet做控制器: 3.前后台交互用MUI的Ajax. 在Hbuilder中选择在安 ...
- .NET项目框架(转)
摘要:本文描述了在用VS.NET进行B/S开发时采用的框架结构,一般建立类库项目和Web项目,在Web基本aspx页面类中调用类库中方法,同时在aspx页面类中不需要写任何对数据库操作的SQL代码,便 ...
- UIWebView的缓存策略,清除cookie
缓存策略 NSURLRequestCachePolicy NSURLRequestUseProtocolCachePolicy缓存策略定义在 web 协议实现中,用于请求特定的URL.是默认的URL缓 ...
- iOS:死锁
死锁:指多个进程因竞争共享资源而造成的一种僵局,若无外力作用,这些进程都将永远不能再向前推进. 安全状态与不安全状态:安全状态指系统能按某个进程顺序来为每个进程分配其所需资源,直至最大需求,使每个进程 ...
- iOS支付
1.IOS purchase 介绍 所谓的IOS 内支付就是在应用中内嵌Store,在iOS APP 中使用Store Kit framework来实现In-App Purchase,Store Ki ...
- HDU 4888 (网络流)
Poroblem Redraw Beautiful Drawings (HDU4888) 题目大意 一个n行m列的矩形,只能填0~k的数字. 给定各行各列的数字和,判定有无合法的方案数.一解给出方案, ...
- 一个自定义的窗体样式MessageBox控件
using System;using System.Collections.Generic;using System.Text;using System.Drawing;using System.Dr ...
- Qt4升级到Qt5
QtWidgets作为一个独立的模块 例如编译时错误 error: QMainWindow: No such file or directory error: QToolButton: No such ...
- vs2013常用快捷键收集
vs2013快捷键: 复制一整行代码: Ctrl+C剪切一整行代码: Ctrl+X删除一整行代码: Ctrl+L跳转到指定的行:ctrl+G 注释:组合键“Ctrl+K+C”取消注释:组合键“Ctrl ...