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 };

    二分查找

  • 相关阅读:
    asp.net2.0中App_GlobalResources的使用
    KPI(Key Performance Indicators )
    Dictionary集合
    SQL连接查询1 内联接查询
    如何在Web.config中注册用户控件和自定义控件
    JavaScript中【数据类型】和【数组类型】的区别
    递归求斐波拉数列函数
    SQL连接查询2 外连接(左右联接查询)
    启示
    “帽子戏法”从何而来?
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7623773.html
Copyright © 2011-2022 走看看