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. Sublime Text 3 注册激活码

    Sublime Text 3 注册激活码 ----- BEGIN LICENSE ----- sgbteam Single User License EA7E-1153259 8891CBB9 F15 ...

  2. Python 条件判断 和循环

    使用条件判断 if else # 条件派单 if else print('条件派单 if else') # s = input('请输入生日年号:') # birth = int(s) birth = ...

  3. Amoeba 实现MySQL读写分离

    Amoeba是一个以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy,它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行.基于此可以实现负载均衡. ...

  4. JDK8~13新特性概览

    JDK8 1. 接口default 与 static 关键字 /** * jdk8中接口可以使用声明default和static修饰的方法 * static修饰的方法和普通的方法一样,可以被直接调用 ...

  5. sql server join联结

    join学习起来有点乱,现做如下整理: table A id abc 1 a 2 b 3 c 4 d table B id abc 1 e 2 a 3 f 4 c --join或者inner join ...

  6. JDK1.8中LinkedList的实现原理及源码分析

    详见:https://blog.csdn.net/cb_lcl/article/details/81222394 一.概述           LinkedList底层是基于双向链表(双向链表的特点, ...

  7. 在linux环境下安装oracle的问题记录

    问题1 xhost:unable to open display 解决办法: 在linux虚拟机本机打开终端,执行 [root@bogon ~]# DISPLAY=:0.0;export DISPLA ...

  8. windows开启ftp服务

    1.启动或关闭windows-->internet information services-->ftp服务器   选中 2.此电脑右键-->管理-->服务和应用程序--> ...

  9. tarjan算法比较详细的讲解&&tarjan常见疑难解答&&洛谷P2002 消息扩散题解

    因为有大佬写的比我更长更具体,所以我也就写写总结一下了 引入: 众所周知,很多图中有个东西名叫环. 对于这个东西很多算法都很头疼.(suchas 迪杰斯特拉) 更深层:环属于强联通分量(strongl ...

  10. LogHelper

    原文链接 public class LogHelper { static string strLogCOMPath = Directory.GetCurrentDirectory() + " ...