码迷,mamicode.com
首页 > 编程语言 > 详细

剑指offer中在一个有序数组中找出和为s的两个数字

时间:2015-04-10 09:32:07      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
using namespace std;
bool findnumberwithsum(int A[],int length,int *num1,int *num2,int key)
{
if(NULL==A||length<=0||NULL==num1||NULL==num2)
return false;
int start=0;
int end=length-1;
int sum=0;
while(start<end)
{
*num1=A[start];
*num2=A[end];
if(*num1+*num2==key)
{
return true;
}
else
{
if(*num1+*num2>key)
{
end--;
}
else
start++;
}
}
return false;
}
int main()
{
int A[]={1,2,4,7,11,15};
int key=15;
int num1;
int num2;
int length=6;
cout<<findnumberwithsum(A,length,&num1,&num2,key)<<endl;
system("pause");
return 0;
}

剑指offer中在一个有序数组中找出和为s的两个数字

标签:

原文地址:http://blog.csdn.net/qq_22335577/article/details/44974967

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!