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

    题目描述

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

    思路:
    头尾指针,向中间走。第一组数据肯定是积最小的。


     1 class Solution {
     2 public:
     3     vector<int> FindNumbersWithSum(vector<int> array,int sum) {
     4         
     5         int i=0,j=array.size()-1;
     6         vector<int> res;
     7         while(i<j)
     8         {
     9             if(array[i]+array[j]<sum)
    10                 i++;
    11             else if(array[i]+array[j]>sum)
    12                 j--;
    13                 else 
    14                 {
    15                     res.push_back(array[i]);
    16                     res.push_back(array[j]);
    17                     return res;
    18                 }
    19                 
    20         }
    21         return res;
    22     }
    23 };
  • 相关阅读:
    E. You Are Given Some Strings...
    神奇函数
    AC自动机再加强版
    AC自动机2
    AC自动机
    three arrays
    permutation 2
    string matching
    permutation 1
    equation
  • 原文地址:https://www.cnblogs.com/SeekHit/p/5846298.html
Copyright © 2011-2022 走看看