LeetCode #1 TwoSum
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 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].
思路
首先我们想到的是暴力法,两个 for 循环求解,时间复杂度为O(n^2)
- //Runtime: 106 ms
- //First thought: BF
- class Solution {
- public:
- vector<int> twoSum(vector<int>& nums, int target) {
- int i,j;
- vector<int> ret {, };
- for (i = ; i< nums.size(); ++i) {
- ret[] = i;
- for(j = i+; j < nums.size(); ++j){
- if (nums[i] + nums[j] == target)
- {
- ret[] = j;
- return ret;
- }
- }
- }
- }
- };
但是,我发现时间消耗太多了,所以借鉴当时“换硬币”、“爬楼梯”问题的优化方法,由于num[i]、num[j]、target都是定值,所以在条件判断里不需要每次都进行加运算
- //Runtime: 79 ms
- //Because nums[i], nums[j], target are fixed values, we do not need to do additions every time
- class Solution {
- public:
- vector<int> twoSum(vector<int>& nums, int target) {
- int i,j;
- vector<int> ret {, };
- for (i = ; i< nums.size(); ++i) {
- ret[] = i;
- int tar = target - nums[i];
- for(j = i+; j < nums.size(); ++j){
- if (nums[j] == tar)
- {
- ret[] = j;
- return ret;
- }
- }
- }
- }
- };
AC后,看了 Discuss 里头的方法以及娄神的博客,我采用了更复杂的数据结构 散列表(HashTable)以降低时间复杂度,我的想法是这样: 如果想只扫描一遍数组就得出结果,那么肯定就要有一部字典,边扫描边存储值,在这里存储的不是数组当前的值,而是“目标值 - 当前值”,我们称之为对象值。
也就是说,字典里存储的是每个数据所希望的”另一半“的大小。所以,字典的 Key 是对象值,字典的 Value 是数组索引。然后我们再往后扫描,如果扫描到的值的另一半出现在了字典里,那么说明当前值是”上一半“所需要的”下一半“,此时将它们的索引存储在 ret[0]、ret[1]中并返回;如果没有字典里没有出现它的另一半,那么把对象值和当前索引继续存储字典中。
该算法的时间复杂度为 O(n)
- //Runtime: 6 ms
- #include<unordered_map>
- using std::unordered_map;
- class Solution {
- public:
- vector<int> twoSum(vector<int>& nums, int target) {
- unordered_map<int,int> um;
- vector<int> res();
- int i;
- int n = nums.size();
- for (i = ; i < n; ++i) {
- if (um.find(target - nums[i]) != um.end()) {
- res[] = um[target - nums[i]];
- res[] = i;
- return res;
- }
- else {
- um[nums[i]] = i;
- }
- }
- um.clear();
- }
- };
补充一种双指针遍历有序数组的方法
- TwoNumberSum (S, x)
- mergeSort (S, , n)
- i =
- j = n
- while i < j
- if A[i] + A[j] == x
- return true
- if A[i] + A[j] < x
- i = i +
- if A[i] + A[j] > x
- j = j -
- return false
可以看得出来,查找的时间仅需 Θ(n),所以时间总代价受排序时间代价的影响,为Θ(n lgn)
扫描的原理很简单,先设 mi,j : A[i] + A[j] < S,Mi,j : A[i] + A[j] > S 。由于序列已排序,则 mi,j ⇒∀k < j 都有 mi,k 成立,并且 Mi,j ⇒ ∀k > i 都有 Mk,j 成立
LeetCode #1 TwoSum的更多相关文章
- Leetcode 1——twosum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- LeetCode 之 TwoSum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- leetcode之twosum
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector& ...
- leetcode ----ARRAY TWOSUM
代码的(判断nums[i]或者是target-nums[i]都可以):
- [LeetCode_1] twoSum
LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- ANDROID学习书单
Skip to content PersonalOpen sourceBusinessExplore Sign upSign in PricingBlogSupport This reposito ...
- Android技能树
第一部分:Android(安卓)Android基础知识Android内存泄漏总结Handler内存泄漏分析及解决Android性能优化ListView详解RecyclerView和ListView的异 ...
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
随机推荐
- Python 爬虫练习(一) 爬取国内代理ip
简单的正则表达式练习,爬取代理 ip. 仅爬取前三页,用正则匹配过滤出 ip 地址和 端口,分别作为key.value 存入 validip 字典. 如果要确定代理 ip 是否真的可用,还需要再对代理 ...
- php 的开发工具
通过上篇我们已经配置好了php的开发环境,我们就可以在这个模拟的环境下运行我们编写的php代码了. 在编写代码前,先安装一个自己喜欢的代码编辑器. 1.sublime text Sublime Tex ...
- Spring JDBC(一)jdbcTemplate
前言 最近工作中经常使用Spring JDBC操作数据库,也断断续续的看了一些源码,便有了写一些总结的想法,希望在能帮助别人的同时,也加深一下自己对Spring JDBC的理解. Spring JDB ...
- Django contrib Comments 评论模块详解
老版本的Django中自带一个评论框架.但是从1.6版本后,该框架独立出去了,也就是本文的评论插件. 这个插件可给models附加评论,因此常被用于为博客文章.图片.书籍章节或其它任何东西添加评论. ...
- #centos7 创建内网yum源 OpenStack源部署
#centos7 创建内网yum源#centos7 自动化安装 本地 内网 web源创建.更新 createrepo http OpenStack源部署 Elven原创 http://www.cnbl ...
- 微软为.NET程序员带来了最优的跨平台开发体验-WSL
前言 在前几个Visual Studio Code更新中发现有一个重要得特性,就是nodejs可以使用VS Code在WSL中进行Debug了(WSL是指Win10中的Linux子系统),之前写过一篇 ...
- poj 3111 K Best 最大化平均值 二分思想
poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...
- HDU1284--完全背包
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Aurora 论坛图片下载
Aurora 论坛图片下载是一款快速下载指定网页图片的利器,还可以下载高清原图呢.现支持的网站:①蜂鸟网论坛②中关村摄影论坛③POCO摄影空间④图虫网其他摄影论坛陆续添加中... 效果图: 项目地址: ...
- 分布式系统中的必备良药 —— RPC
阅读目录 前言 成熟的解决方案 剖析 性能测试 结语 一.前言 在上一篇分布式系统系列中<分布式系统中的必备良药 —— 服务治理>中阐述了服务治理的一些概念,那么与服务治理配套的必然会涉及 ...