[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 ...
随机推荐
- angularjs执行流程
angularjs源码分析之:angularjs执行流程 angularjs用了快一个月了,最难的不是代码本身,而是学会怎么用angular的思路思考问题.其中涉及到很多概念,比如:directi ...
- Ecological Premium - UVa10300
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10300.html 题目描述 Pr ...
- SZU:A66 Plastic Digits
Description There is a company that makes plastic digits which are primarily put on the front door o ...
- The initialization of the CRM authentication pipline execution has failed
由于公司电路切换,昨天晚上不得不将服务器暂时关闭.早上重新开机时,发现开发环境连不上了.这可把我急坏了,大家可都等着开发呢. 于是查看服务器错误消息,发现时数据库连接连接不上. The initial ...
- 安装升级System.Web.Optimization.dll
今天在使用backload时,VS提示solution所引用的System.Web.Optimization.dll 版本低,编译不过,于是便删掉,从新添加引用,悲剧的是在添加引用窗口中没找到,在Nu ...
- wpf中数据绑定(Datacontext)的应用
在winform开发中,我们常用到ado.net进行数据绑定,在编程技术日新月异的今天,这种繁杂的数据绑定方式已不能再适合开发人员,于是微软推出了wpf,更炫的界面美化,更简洁地编写控件,在wpf中使 ...
- DSP TMS320C6000基础学习(4)—— cmd文件分析
DSP中的CMD文件是链接命令文件(Linker Command File),以.cmd为后缀. 在分析cmd文件之前,必需先了解 (1)DSP具体芯片的内存映射(Memory Map) (2)知道点 ...
- 向输出到console的文字加样式
Chrome 控制台新玩法-向输出到console的文字加样式 有兴趣的同学可以文章最后的代码复制贴到控制台玩玩. Go for Code 在正常模式下,一般只能向console 控制台输出简单的 ...
- Struts2更改配置文件struts.xml默认路径
struts2配置文件默认存放路径在/WEB-INF/classes目录下,即将struts.xml放在src的目录下. 但是为了协作开发与方便管理,我们有时需要把struts.xml放到其他位置 s ...
- StringEscapeUtils.unescapeHtml的使用
在做代码高亮时,从数据库中取出代码如下(节选): <pre class="brush: java;"> 需要的应该是<pre class=\"brush ...