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 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
随机推荐
- MAC PHP MARK
这是一篇以 iOS 开发人员的视角写给广大iOS 程序猿的 PHP 入门指南.在这篇文章里我努力去发掘 objectiv-c 与 php 之间的共性,来帮助有一定 iOS 开发经验的攻城狮来快速上手一 ...
- 并非然并卵的z-index
最近做一些东西的时候总觉得加上z-index和不加对于最终的显示结果并没有什么区别,开始以为一张图片把z-inde的值调小一点儿,就可以当做背景图片一样使用,跟background是一样的,在试过几次 ...
- Git ~ 管理修改 ~ Gitasd
现在假设你一经常我了暂存区的概念 , 下面我们将要讨论的就是 , 为什么 Git 比其他的版本控制系统设计的优秀 , 因为 Git 跟踪管理的是修改而非文件 什么是修改 ? 修改就是 你在某个地方 ...
- linux命令:touch
1:命令介绍: touch用来创建文件或修改文件和目录的时间戳,包括存取时间和更改时间. 2:命令格式: touch [选项] 文件 3:命令参数: -a 或--time=atime或--time ...
- 对前端mvc的认识和思考
现在,我们经常都可以看到复杂的JavaScript应用程序,由于这些应用程序变得越来越复杂,一长串的jQuery回调语句或者通过应用程序在 各个状态执行不同的函数调用,这些做法都会变得无法再让人接受, ...
- NSArray,NSSet,NSDictionary的遍历,基本使用集锦
NSArray *array = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu&quo ...
- The Cow Lineup_找规律
Description Farmer John's N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled ...
- 第一个Sprint冲刺第六天
讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:解决编写代码的问题 讨论地点:宿舍 进展:已开始对代码的编写
- busybox reboot 无效
/*********************************************************************** * busybox reboot 无效 * 说明: * ...
- C++学习笔记5:如何给变量及函数命名?
1.遵循C++规定的变量及函数命名方法: 2.原则:简单,易于理解: 以下是一些例子,可以作为参考: //bad examples: int ccount;//Nobody knows what a ...