zoukankan      html  css  js  c++  java
  • 167. Two Sum II

    167. Two Sum II - Input array is sorted【easy】

    Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

    The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

    You may assume that each input would have exactly one solution and you may not use the same element twice.

    Input: numbers={2, 7, 11, 15}, target=9
    Output: index1=1, index2=2

    解法一:

     1 class Solution {
     2 public:
     3     vector<int> twoSum(vector<int>& numbers, int target) {
     4         vector<int> result;
     5         
     6         for (int i = 0, j = numbers.size() -1; i <= j;) {
     7             if (numbers[i] + numbers[j] == target) {
     8                 result.push_back(i + 1);
     9                 result.push_back(j + 1);
    10                 return result;
    11             }
    12             else if (numbers[i] + numbers[j] > target) {
    13                 --j;
    14             }
    15             else if (numbers[i] + numbers[j] < target) {
    16                 ++i;
    17             }
    18         }
    19             
    20         return result;
    21     }
    22 };

    双指针

    解法二:

     1 class Solution {
     2 public:
     3     vector<int> twoSum(vector<int>& numbers, int target) {
     4         vector<int> result;
     5         
     6         for (int i = 0; i < numbers.size() - 1; ++i) {
     7             int start = i + 1;
     8             int end = numbers.size() - 1;
     9             int temp_target = target - numbers[i];
    10             
    11             //binary search
    12             while (start + 1 < end) {
    13                 int mid = start + (end - start) / 2;
    14                 if (numbers[mid] == temp_target) {
    15                     result.push_back(i + 1);
    16                     result.push_back(mid + 1);
    17                     return result;
    18                 }
    19                 else if (numbers[mid] > temp_target) {
    20                     end = mid;
    21                 }
    22                 else if (numbers[mid] < temp_target) {
    23                     start = mid;
    24                 }
    25             }
    26             if (numbers[start] == temp_target) {
    27                 result.push_back(i + 1);
    28                 result.push_back(start + 1);
    29                 return result;
    30             }
    31             if (numbers[end] == temp_target) {
    32                 result.push_back(i + 1);
    33                 result.push_back(end + 1);
    34                 return result;
    35             }          
    36         }
    37             
    38         return result;
    39     }
    40 };

    二分查找

  • 相关阅读:
    某耳机厂商微信公众平台建设方案
    微信公众号订单好评红包返现系统
    LNMP的配置与优化
    微信公众号用户OpenID同步导出系统
    微信公众号无限定时群发系统
    小程序想要什么
    微信支付默认推荐关注规则
    微信小程序(应用号)资源汇总整理
    微信公众平台小程序开发教程
    微信应用号开发教程
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7623773.html
Copyright © 2011-2022 走看看