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

    二分查找

  • 相关阅读:
    log4net logfornet 配置和用法
    seaJs学习笔记
    docker 创建tomcat镜像
    配置alibaba的yum 源
    maven 中 jar管理
    docker 创建elasticsearch集群镜像
    springboot 常用插件
    elasticsearch插件安装之--linux下安装及head插件
    docker 创建jdk镜像
    docker 创建基础镜像
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7623773.html
Copyright © 2011-2022 走看看