zoukankan      html  css  js  c++  java
  • 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 };
  • 相关阅读:
    DDD:四色原型中Role的 “六” 种实现方式
    .NET:脏读、不可重复读和幻读测试
    AIR:使用 HTML + Javascript 开发桌面应用
    Silverlight:《Pro Silverlight5》读书笔记 之 Layout
    设计原则:什么样的情况下需要引入父类?
    设计原则:不要为了复用而使用继承
    Ruby:字符集和编码学习总结
    .NET:字符集和编码学习总结
    Ruby:Sublime中开发Ruby需要注意的Encoding事项
    .NET:遇到并发问题,什么样的情况下需要自动重试?
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5839916.html
Copyright © 2011-2022 走看看