01Two Sum题解

Tow Sum

原题概述:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.



Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,

return [0, 1].



题目大意:

  1. 题目给出一个不一定有序的整型数组和一个数值target。
  2. 在数组中找到两个数加起来 == target,返回其下标。
  3. 题目保证答案只有一个,并且数组中的每个数只能用一次.

解答:

整理一下思路:

要找到两数和为指定数值,如果用双层for循环,那么时间复杂度将会达到O(n)级别。

考虑一种思路:如果数组有序,采用双指针的思想,我们从第一个元素+最后一个元素开始判断。

(1) 如果第一个元素+最后一个元素 > target,那么指向最后一个元素的指针移动到倒数第二个元素。

(2) 如果第一个元素 + 最后一个元素 < target,那么指向第一个元素的指针移动到第二个位置。

(3) 如果正好相等,直接返回。时间复杂度就达到O(n)级别(是在有序的情况下)。但是要让数组有序,利用快排等方法时间复杂度为 nlogn.

附上Accept代码:

class Solution
{
public:
    vector<int> twoSum(vector<int>& nums, int target)
    {
        vector<int> vctResult;
        if(nums.size() < 2)
        {
            return vctResult;
        }
        if(nums.size() == 2)
        {
            vctResult.push_back(0);
            vctResult.push_back(1);
            return vctResult;
        }

        vector<int> vct(nums.begin(),nums.end());
        sort(vct.begin(),vct.end());
        int iStart = 0, iEnd = nums.size() - 1;
        while(iStart < iEnd)
        {
            if(vct[iStart] + vct[iEnd] == target)
            {
                iStart = vct[iStart];
                iEnd = vct[iEnd];
                break;
            }
            else if(vct[iStart] + vct[iEnd] < target)
            {
                ++iStart;
            }
            else
            {
                --iEnd;
            }
        }
        for(unsigned int i = 0; i < vct.size(); ++i)
        {
            if(nums[i] == iStart)
                vctResult.push_back(i);
            else if(nums[i] == iEnd)
                vctResult.push_back(i);
        }
        return vctResult;
    }
};

原文地址:https://www.cnblogs.com/love-jelly-pig/p/9595281.html

时间: 2024-10-06 08:17:54

01Two Sum题解的相关文章

LeetCode: Two Sum 题解

Given an array of integers, 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

leetcode1 Two Sum题解

题目大概意思就是,我给你传进来一个vector容器和一个target,vector相当于一个数组,现在问target是有数组种哪两个数组成的,返回两个下标,注意函数的返回类型也是vector类型的,所以一定要注意. 题目刚到手的时候,发现这个与各大OJ套路不太一样啊,也就是与ACM不太一样,我还傻傻的调整输出格式什么的,而且这个是完善一个类的成员函数,而不是提交一个可以运行的完整代码,觉得还是挺新奇的. 思路分析 最开始的时候,以为这个如果数据量不是很大的时候可以桶排序暴力破解啊,我见建立了一个

Hdoj 1003.Max Sum 题解

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains

LeetCode 1 Two Sum 题解

Problem: Given an array of integers, 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 n

HDU1003 Max Sum 题解 动态规划 最大字段和扩展

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意: 求解一个序列的最大字段和,已经最前面的那个最大子段的起止坐标. 解题思路: 定义状态 \(f[i]\) 为以 \(a[i]\) 结尾的最大字段和,则有状态转移方程: \[f[i] = \max(0, f[i-1]) + a[i]\] 同时定义状态 \(s[i]\) 表示以 \(a[i]\) 结尾的最大字段的最左边元素的坐标,则有: 当 \(f[i] \lt 0\) 时,\(s[i]

CF33C Wonderful Randomized Sum 题解

原题链接 简要题意: 你可以无限次的把该数组的一个前缀和后缀 \(\times -1\),问最终的最大序列和. 这题盲目WA了数次才知道本质 这题89个数据吊打std CF真好啊,发现一个错后面就不测了 下面,就以我艰辛的思维历程来构造本篇博客. 算法一 盲猜:所有数都可以变成正数. 然后绝对值相加. 连样例也没测. 然后,\(\frac{2}{89} pts\). 只过了前两个样例,第三个就死了. #pragma GCC optimize(2) #include<bits/stdc++.h>

HLJU14级贪心训练题解

由于有的题是很久之前写的题解(主要是我太懒了~~~),所以这里就不从新写了,直接给大家链接了^_^ A--最少拦截系统 题解链接:最少拦截系统 B--今年暑假不AC 题解链接:今年暑假不AC C--Tian Ji -- The Horse Racing 题解链接:Tian Ji -- The Horse Racing D--Max Sum 题解链接:Max Sum PS:有问题的,可以在群里讨论,或者是通过QQ问我,附上本弱QQ:1426137347   ^_^

[Leetcode] Sum 系列

Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one sol

Codeforces Round #250 (Div. 2)B. The Child and Set 暴力

B. The Child and Set At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks. Fortunately, Picks remembers something about h