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

01Two Sum题解的更多相关文章

  1. Ural 1248 Sequence Sum 题解

    目录 Ural 1248 Sequence Sum 题解 题意 题解 程序 Ural 1248 Sequence Sum 题解 题意 给定\(n\)个用科学计数法表示的实数\((10^{-100}\s ...

  2. LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表

    文章目录 题意 思路 特殊情况k=0 Source Code 1 Source Code 2 题意 给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数. 思路 和上一篇博 ...

  3. 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 ...

  4. [LeetCode]Combination Sum题解(DFS)

    Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T), f ...

  5. [LeetCode] Three Sum题解

    Three Sum: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? ...

  6. 【CF1445D】Divide and Sum 题解

    题目链接 题意简介 将一个长度为 2n 的数列平均分为两个子数列 p 和 q 后,p 按从小到大排序,q 按从大到小排序. 排序后,记 p 为 \(\{x_i\}\) ,q 为 \(\{y_i\}\) ...

  7. BZOJ3155:Preprefix sum——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3155 最朴素的想法是两棵树状数组,一个记录前缀和,一个记录前缀前缀和,但是第二个我们非常不好修改 ...

  8. HDU4825:Xor Sum——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=4825 Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含 ...

  9. 洛谷 P2398 GCD SUM 题解

    题面 挺有意思的. 设f[i]表示gcd(i,j)=i的个数,g[i]表示k|gcd(i,j)的个数; g[i]=(n/i)*(n/i); g[i]=f[i]+f[2i]+f[3i]+...; 所以f ...

随机推荐

  1. ASP.NET Core中使用EasyCaching作为缓存抽象层

    ⒈是什么? 和CacheManager差不多,两者的定位和功能都差不多. EasyCaching主要提供了下面的几个功能 统一的抽象缓存接口 多种常用的缓存Provider(InMemory,Redi ...

  2. c++学习笔记之类和对象(二、构造函数和析构函数)

    1.构造函数(Constructor):在C++中,有一种特殊的成员函数,它的名字和类名相同,没有返回值,不需要用户显式调用(用户也不能调用),而是在创建对象时自动执行. 这种特殊的成员函数就是构造函 ...

  3. 从入门到自闭之Python闭包

    闭包 定义:在嵌套函数内,使用(非本层变量)非全局变量就是闭包 闭包必须是内层函数对外层函数的变量(非全局变量)的引用 函数执行完毕后,函数体内的空间自行销毁 def func(): a=1 def ...

  4. Android新版xUtils3工具类相关debug

    首先出现问题是 build.gradle中的csayısıom.lidroid.xutils:xutils:2.6.13报错了,所以想到是版本的问题,github上搜了xutils发现有新版xutil ...

  5. FluentValidation在C# WPF中的应用

    原文:FluentValidation在C# WPF中的应用 一.简介 介绍FluentValidation的文章不少,零度编程的介绍我引用下:FluentValidation 是一个基于 .NET ...

  6. ITCAST-C# 委托

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _12委 ...

  7. 学习笔记--最近公共祖先(LCA)的几种求法

    前言: 给定一个有根树,若节点\(z\)是两节点\(x,y\)所有公共祖先深度最大的那一个,则称\(z\)是\(x,y\)的最近公共祖先(\(Least Common Ancestors\)),简称\ ...

  8. Sklearn使用良心完整入门教程

    The complete .ipynb file can be download through my share in onedrive:https://1drv.ms/u/s!Al86h1dThX ...

  9. 使用pt-table-checksum检查主从一致性

    使用 percona 工具检查主从不一致 可以使用 pt-table-checksum  工具检查主从数据的一致性,检查完之后默认会生成一个 percona 库以及一个 checksums 表,记录了 ...

  10. JVM内存区域划分总结

    发现网上有两个版本的JVM内存划分,一个是按照<深入理解JVM虚拟机>上的版本,包含程序计数器等,按照是否线程共享划分. 另一个我觉得更好记一些,也更适合我自己,在这里记录一下. 首先上思 ...