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

    二分查找

  • 相关阅读:
    mybatis中的配置文件的约束
    win10下PHP开发环境搭建
    装饰器的理解
    在iis上添加woff字体文件读取
    转发:使用sql命令查询视图中所有引用的基础表
    转:C4项目中验证用户登录一个特性就搞定
    转载:NSobject官方介绍
    thinkphp生命周期
    array_intersect_assoc — 带索引检查计算数组的交集
    array_flip — 交换数组中的键和值
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7623773.html
Copyright © 2011-2022 走看看