剑指offer—和为s的两个数字
来源:互联网 发布:数学建模常用算法 编辑:程序博客网 时间:2024/05/22 16:46
题目描述
输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
import java.util.ArrayList;public class Solution { public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) { ArrayList<Integer> res = new ArrayList<Integer>(); if(array==null || array.length<=0) return res; int index1 = 0; int index2 = array.length-1; while(index1<index2){ if(array[index1]+array[index2]==sum){ res.add(array[index1]); res.add(array[index2]); break; }else if(array[index1]+array[index2]<sum){ index1++; }else{ index2--; } } return res; }}
思路:由于数组是递增的,因此可以使用一头一尾两个指针,如果两个指针对应的数组值等于目标值则返回,如果大于目标值,则后面的指针前移,反之,前面的指针后移
阅读全文
0 0
- 《剑指offer》——和为S的两个数字
- 剑指offer——和为S的两个数字
- 剑指offer——和为S的两个数字
- 剑指offer——和为S的两个数字
- 剑指offer—和为S的两个数字
- 剑指offer—-和为S的两个数字
- 剑指offer—和为s的两个数字
- 剑指offer — 和为S的两个数字
- 剑指offer系列源码-和为S的两个数字
- 剑指offer--和为S的两个数字
- 《剑指offer》和为S的两个数字
- 剑指·offer:和为S的两个数字
- 剑指offer-和为S的两个数字
- 剑指offer:和为S的两个数字
- 【剑指offer系列】 和为S的两个数字___41
- 剑指offer(43):和为s的两个数字
- 剑指offer:和为S的两个数字
- 剑指offer 和为S的两个数字
- 常见内排序实现汇总(含部分优化实现,基于链表的实现),以及性能比较
- Leetcode: 40. Combination Sum II(Week13, Medium)
- 文章标题
- CheBox
- ISCSI设备共享
- 剑指offer—和为s的两个数字
- HTC Vive手柄圆盘控制角色移动
- 阿里杨勇:浅谈 Linux 高负载的系统化分析
- 辅助类
- NVIDIA TK1 学习笔记(8):自动连接wifi
- 关于用spring-Mybatis用maven构建框架
- 距离和相似度度量方法
- 关于wpf设置属性的几种方式
- PXE全自动远程安装