zoukankan      html  css  js  c++  java
  • 剑指offer——和为S的两个数字

    题目描述

    输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。

    输出描述:

    对应每个测试案例,输出两个数,小的先输出。
     1 import java.util.ArrayList;
     2 public class Solution {
     3     public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) {
     4         ArrayList<Integer> result = new ArrayList<Integer>();
     5         if(array==null||array.length<2)
     6             return result;
     7         int plow=0,phigh=array.length-1;
     8         while(phigh>plow){
     9             int cur=array[phigh]+array[plow];
    10             if(cur==sum){
    11                 result.add(array[plow]);
    12                 result.add(array[phigh]);
    13                 return result;
    14             }else if(cur<sum){
    15                 plow++;
    16             }else{
    17                 phigh--;
    18             }
    19         }
    20         return result;
    21     }
    22 }
  • 相关阅读:
    mysql 索引
    mysql binlog相关知识
    分布式系统日志
    学习路线
    分布式学习
    工具
    关于java面试
    mysql悲观锁总结和实践(转)
    app技术博客整理
    Java编程一些经验
  • 原文地址:https://www.cnblogs.com/10081-AA/p/11348384.html
Copyright © 2011-2022 走看看