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的更多相关文章

  1. 对于SQL的Join,在学习起来可能是比较乱的。我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚。Coding Horror上有一篇文章,通过文氏图 Venn diagrams 解释了SQL的Join。我觉得清楚易懂,转过来。

     对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codi ...

  2. Understanding User and Kernel Mode

    https://blog.codinghorror.com/understanding-user-and-kernel-mode/ Continue Discussion92 repliesJan ' ...

  3. bash编程之多分支if 语句及for循环

    第十七章.bash编程之多分支if 语句及for循环 if语句三种格式 多分支if语句练习 for循环 17.1.if语句的三种格式 单分支if语句 if condition;then 条件为真执行的 ...

  4. [No00008C]图解SQL的各种连接join让你对SQL的连接一目了然

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  5. 图解SQL的Join 转自coolshell

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  6. 图解SQL的Join(转)

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  7. (转)[BetterExplained]为什么你应该(从现在开始就)写博客

    (一)为什么你应该(从现在开始就)写博客 用一句话来说就是,写一个博客有很多好处,却没有任何明显的坏处.(阿灵顿的情况属于例外,而非常态,就像不能拿抽烟活到一百岁的英国老太太的个例来反驳抽烟对健康的极 ...

  8. 最牛B的编码套路 - 呦呦鹿鸣 - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  9. 关于sql中join

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

随机推荐

  1. Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定

    使用Knockout, Web API 和 ASP.Net Web Forms 进行简单数据绑定   原文地址:http://www.dotnetjalps.com/2013/05/Simple-da ...

  2. jquery 实现飘落效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. [google面试CTCI] 1-8.判断子字符串

    [字符串与数组] Q:Assume you have a method isSubstring which checks if one word is a substring of another G ...

  4. Apache Rewrite 中文详解

    这几天一直在研究Apache的重写规则,虽然网上有很多教程,不过发现大部分都是抄袭一个人的,一点都不全,所以我想写一个简单的易于理解的教程,我学习.htaccess是从目录保护开始的,这个也比较简单, ...

  5. xTree学习

    1.XTREE简介: XTREE是一个基于AJAX实现的树形菜单.它的原理就是每次都只加载当前结点下的所有结点,而对开发人员来说,就是只需要按一定的格式,生成一段XML代码.XTREE可以自己定制每个 ...

  6. 系统架构、网络通信、IM、视频会议技术

    专注于系统架构.网络通信.IM.视频会议技术. 主要作品: ESFramework 强悍的通信框架.P2P框架.群集平台. OMCS 简单易用的 网络语音视频 框架. MFile 语音视频录制组件. ...

  7. eclipse plugin 导出插件包

    当我们的插件在完成一个阶段性开发的时候,我们要发布一个1.0的版本.这个时候会碰到一个问题.如何把我们的插件打成包?有多种途径,下面具体讨论一下. 首先从插件完成到被他人(或者我们自己)使用有两个步骤 ...

  8. 如何利用.Net内置类,解析未知复杂Json对象

    如何利用.Net内置类,解析未知复杂Json对象 如果你乐意,当然可以使用强大的第三方类库Json.Net中的JObject类解析复杂Json字串 . 我不太希望引入第三方类库,所以在.Net内置类J ...

  9. js中的“闭包”

    js中的“闭包” 姓名:闭包 官方概念:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. ( ⊙o⊙ )!!!这个也太尼玛官方了撒,作为菜鸟的 ...

  10. 从UI开始

    虚拟化平台cloudstack(8)——从UI开始   UI ucloudstack采用的是前后端分离的架构,就是说前端可以选择使用web.swing甚至其它的界面,都可以. 我们来看cloudsta ...