• Leetcode 167 Two Sum II


    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.

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

    典型的双指针问题。

    初始化左指针left指向数组起始,初始化右指针right指向数组结尾。

    根据已排序这个特性,

    (1)如果numbers[left]与numbers[right]的和tmp小于target,说明应该增加tmp,因此left右移指向一个较大的值。

    (2)如果tmp大于target,说明应该减小tmp,因此right左移指向一个较小的值。

    (3)tmp等于target,则找到,返回left+1和right+1。(注意以1为起始下标)

    时间复杂度O(n): 扫一遍

    空间复杂度O(1)

    c代码:

     1 /**
     2  * Return an array of size *returnSize.
     3  * Note: The returned array must be malloced, assume caller calls free().
     4  */
     5 int* twoSum(int* numbers, int numbersSize, int target, int* returnSize) {
     6         int* a = (int*)malloc(2 * sizeof(int));
     7         *returnSize = 2;
     8         int l = 0, r = numbersSize - 1;
     9         //由于是升序,先用二分查找,找到第一个大于target值的下标,让l重新指向0.然后再用双指针。
    10         while(r - l > 1){
    11             int m = (r - l) / 2 + l;
    12             if(numbers[m] > target)
    13                 r = m;
    14             else
    15                 l = m;
    16         }
    17         l = 0;
    18         //这里开始利用双指针查找
    19         while(l < r){
    20             int sum = numbers[l] + numbers[r];
    21             if(sum == target){
    22                 a[0] = l + 1;
    23                 a[1] = r + 1;
    24                 break;
    25             } else if (sum > target){
    26                 r--;
    27             } else {
    28                 l++;
    29             }
    30         }
    31         return a;
    32 }

    c++代码:

     1 class Solution {
     2 public:
     3     vector<int> twoSum(vector<int>& numbers, int target) {
     4         vector<int> v;
     5         int n = numbers.size();
     6         int l = 0, r = n - 1;
     7         while(r - l > 1){
     8             int m = (r - l) / 2 + l;
     9             if(numbers[m] > target)
    10                 r = m;
    11             else
    12                 l = m;
    13         }
    14         l = 0;
    15         while(l < r){
    16             int sum = numbers[l] + numbers[r];
    17             if(sum == target){
    18                 v.push_back(l + 1);
    19                 v.push_back(r + 1);
    20                 break;
    21             } else if (sum > target){
    22                 r--;
    23             } else {
    24                 l++;
    25             }
    26         }
    27         return v;
    28     }
    29 };
  • 相关阅读:
    【动态规划】数的划分
    【动态规划】开心的小明
    【动态规划】回文字符串
    【动态规划】skiing_深度搜索_动态规划
    【动态规划】最大子串和
    JDBC中 execute 与 executeUpdate的区别
    poj 2449 Remmarguts' Date 求第k短路 Astar算法
    ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer 最大生成树 lca
    hdu 2586 How far away ? 倍增求LCA
    acm模板生成
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5839916.html
Copyright © 2020-2023  润新知