• 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 };
  • 相关阅读:
    函数特化
    模板函数总结
    学习代码1
    宏指令
    #define宏作用
    oracle 重要函数
    JMeter 系列之—-01 使用
    Selenium系列之--03 常见元素操作总结
    【转】TestNG常用注解
    CMMI 2,3,4,5级涉及的过程域(PA)介绍
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5839916.html
Copyright © 2020-2023  润新知