zoukankan      html  css  js  c++  java
  • 剑指offer-面试题57_1-和为s的两个数字-双指针

    /*
    题目:
    	输入一个递增数组和一个s,求和等于s的两个数组中的数字。
    */
    /*
    思路:
    	双指针问题。
    */
    #include<iostream>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    #include<map>
    
    using namespace std;
    
    vector<int> FindNumbersWithSum(vector<int> array,int sum) {
        vector<int> res;
    
        int left = 0, right = array.size() - 1;
    
        while(right > left){
            int temp = array[left] + array[right];
            if(temp == sum){
                res.push_back(array[left]);
                res.push_back(array[right]);
                break;
            }else if(temp > sum){
                right--;
            }else{
                left++;
            }
    
        }
        return res;
    }
    
    int main(){
        vector<int> data = {1,2,4,7,11,15};
        vector<int> res = FindNumbersWithSum(data,15);
        cout<<res[0]<<" "<<res[1];
    }
    

       

  • 相关阅读:
    CF Round433 B. Jury Meeting
    CF Round433 C. Planning
    繁忙的都市
    联络员
    组合数模板
    Rinne Loves Xor
    [SDOI2016]齿轮
    水题(water)
    Music Problem
    小H和游戏
  • 原文地址:https://www.cnblogs.com/buaaZhhx/p/12108431.html
Copyright © 2011-2022 走看看