[coding horror] 1 - sum 2
sum 2
描述
输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
对应每个测试案例,输出两个数,小的先输出。
coding horror
class Solution {
public:
vector<int> FindNumbersWithSum(vector<int> array,int sum) {
vector<int> ret;
if(array.size()==0){
return ret;
};
vector<int> first_nums;
vector<int> second_nums;
int first_index = 0;
int second_index = array.size() - 1;
while(first_index < second_index){ // index越界
int first = array[first_index];
int second = array[second_index];
if( first + second == sum) {
first_nums.push_back(first);
second_nums.push_back(second);
first_index++;
second_index--;
}
else if(first + second < sum){
first_index++;
}
else{ // first + second > sum
second_index--;
}
}
int min_index = 0;
int min; // 未初始化最小值
for(int i =0 ;i < first_nums.size(); i++){
if(first_nums[i]*second_nums[i] < min){
min_index = i;
min = first_nums[i]*second_nums[i];
}
}
ret.push_back(first_nums[min_index]);
ret.push_back(second_nums[min_index]);
return ret;
}
};
maybe ok
#include "stdio.h"
#include <vector>
using std::vector;
class Solution {
public:
vector<int> FindNumbersWithSum(vector<int> array,int sum) {
vector<int> ret;
if(array.size()==0){
return ret;
};
vector<int> first_nums;
vector<int> second_nums;
int first_index = 0;
int second_index = array.size() - 1;
while((first_index < second_index)&&(first_index <array.size()-1)&&(second_index>0)){// 注意索引不要越界。
int first = array[first_index];
int second = array[second_index];
if( first + second == sum) {
first_nums.push_back(first);
second_nums.push_back(second);
first_index++;
second_index--;
}
else if(first + second < sum){
first_index++;
}
else{ // first + second > sum
second_index--;
}
}
if(first_nums.size()>0){
int min_index = 0;
int min = first_nums[0]*second_nums[0];// 1.如果vector为空可能造成越界,需先判断vector是否为空。2. 用于比较的最小值要先赋初始值。
for(int i =0 ;i < first_nums.size(); i++){
if(first_nums[i]*second_nums[i] < min){
min_index = i;
min = first_nums[i]*second_nums[i];
}
}
ret.push_back(first_nums[min_index]);
ret.push_back(second_nums[min_index]);
}
return ret;
}
};
int main(int argc, char const *argv[])
{
/* code */
int nums[] = {1,1,1,3,1};
std::vector<int> vnums(nums,nums+5);
Solution sol;
std::vector<int> vret;
vret = sol.FindNumbersWithSum(vnums,5);
for(std::vector<int>::iterator it= vret.begin(); it != vret.end();it++){
printf("%d\n", *it);
}
return 0;
}
[coding horror] 1 - sum 2的更多相关文章
- 对于SQL的Join,在学习起来可能是比较乱的。我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚。Coding Horror上有一篇文章,通过文氏图 Venn diagrams 解释了SQL的Join。我觉得清楚易懂,转过来。
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codi ...
- Understanding User and Kernel Mode
https://blog.codinghorror.com/understanding-user-and-kernel-mode/ Continue Discussion92 repliesJan ' ...
- bash编程之多分支if 语句及for循环
第十七章.bash编程之多分支if 语句及for循环 if语句三种格式 多分支if语句练习 for循环 17.1.if语句的三种格式 单分支if语句 if condition;then 条件为真执行的 ...
- [No00008C]图解SQL的各种连接join让你对SQL的连接一目了然
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
- 图解SQL的Join 转自coolshell
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
- 图解SQL的Join(转)
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
- (转)[BetterExplained]为什么你应该(从现在开始就)写博客
(一)为什么你应该(从现在开始就)写博客 用一句话来说就是,写一个博客有很多好处,却没有任何明显的坏处.(阿灵顿的情况属于例外,而非常态,就像不能拿抽烟活到一百岁的英国老太太的个例来反驳抽烟对健康的极 ...
- 最牛B的编码套路 - 呦呦鹿鸣 - 博客频道 - CSDN.NET
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- 关于sql中join
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...
随机推荐
- Oracle中的Union、Union All、Intersect、Minus
Oracle中的Union.Union All.Intersect.Minus 众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考. 假设我们有一个表Student,包括 ...
- [转]How To Use CSS3 Media Queries To Create a Mobile Version of Your Website
CSS3 continues to both excite and frustrate web designers and developers. We are excited about the p ...
- Git的使用学习资源
开学第一天一般都挺认真的,认真做个功课. 跟据Ryan Tang的推荐,有两个比较好的学习Git的网站:http://git.gitcafe.com/book/zh 还有一个是CodeSchool的一 ...
- Oracle组函数、多表查询、集合运算、数据库对象(序列、视图、约束、索引、同义词)等
count组函数:(过滤掉空的字段) select count(address),count(*) from b_user max() avg() min(),sum() select sum(age ...
- Aforge.net 一个专门为开发者和研究者基于C#框架设计
时间过得真快啊,转眼今年就要过去了,大半年都没有写博客了,要说时间嘛,花在泡妹子和搞英语去了,哈哈...前几天老大问我 怎么这么长时间都没写博客了,好吧,继续坚持,继续分享我的心得体会. 这个系列我们 ...
- Python语言在企业级应用上的十大谬误
英文原文:https://www.paypal-engineering.com/2014/12/10/10-myths-of-enterprise-python/ 翻译原文:http://www.os ...
- dapper 扩展插件: Rainbow
dapper 扩展插件: Rainbow dapper 是一个效率非常高的orm 框架 ,效率要远远大于 我们大微软的EF . 它只有一个类文件,非常之小. 1,首先下载dapper 这里下 ...
- PHP中的赋值-引用or传值?
直接上代码: <?php $num1 = 1; $num2 = $num1; $num1 = 2; echo $num2 . "\n"; $arr1 = array(1, 2 ...
- 调用MobileAPI的设计(iOS篇)
调用MobileAPI的设计(iOS篇) 这一节讲如何发起网络请求. iOS用于调用MobileAPI的第三方组件很多,我们这里采用的是以下组件: 1)ASIHTTPRequest,用于请求Mobil ...
- Scope and Namespace
Python基础-作用域和命名空间(Scope and Namespace) 在Python中,对象是独立的,不同作用域中的不同名字都可以被绑定在同一个对象上,当然对这个对象的修改会影响所有的引用.赋 ...