和为s的两个数字
来源:互联网 发布:手机所有交友软件 编辑:程序博客网 时间:2024/04/29 20:24
输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
#include <iostream>#include <vector>using namespace std;class Solution { public: vector<int> FindNumbersWithSum(vector<int> array,int sum) { vector<int> res; if (array.size() == 0) return res; int length = array.size(); int low = 0; int high = length - 1; int putLow = low; int putHigh = high; int multi = 0x7fffffff; bool find = false; while (low < high) { long long curSum = array[low] + array[high]; if (curSum == sum) { find = true; if (array[low]*array[high] < multi) { multi = array[low]*array[high]; putLow = low; putHigh = high; } high--; } else if (curSum < sum) low++; else high--; } if(find == true) { res.push_back(array[putLow]); res.push_back(array[putHigh]); } return res; }};
0 0
- 和为S的两个数字
- 和为S的两个数字
- 找出和为S的两个数字
- 和为S的两个数字
- 【5】和为s的两个数字
- 和为S的两个数字
- 和为s的两个数字
- 和为s的两个数字
- 和为S的两个数字
- 和为S的两个数字
- 和为S的两个数字
- 和为s的两个数字
- 和为s的两个数字
- 牛客网 | 和为S的两个数字
- 和为s的两个数字
- 和为S的两个数字
- 和为S的两个数字
- 和为S的两个数字
- 重温C语言之路
- LEETCODE-Palindrome Linked List
- 5中服务器的网络编程
- Java继承、接口、试卷解析小结
- LSB-Tree:Locality Sensitive B-Tree原理分析
- 和为s的两个数字
- 支付宝个人收款二维码的创建
- Android中fragment模式
- C语言卷积和C语言互相关:数字信号处理中的卷积和互相关的C语言代码
- C#注册表的读写
- 韩顺平 java 第二十讲 二进制、位运算、移位运算符
- 析构的问题
- 二叉树层次遍历和深度遍历
- sql 2012 与java连接的类