一天一道LeetCode系列

(一)题目

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.

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

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

(2016/2/13): The return format had been changed to zero-based indices.

Please read the above updated description carefully.

题目的意思是:输入一组数组和一个目标值,在数组中找出两个数,他们的和等于目标值,返回两个数的下标。

(二)代码实现

初看题目我们可以很快得到以下的答案:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> result;
        for(int i = 0 ; i < nums.size() ; ++i)
        {
            for(int j = i+1 ; j<nums.size() ; ++j)
            {
                if(nums[i]+nums[j] == target)
                {
                    result.push_back(i);
                    result.push_back(j);
                    return result;
                }
            }
        }
    }
};

这样的解法的时间复杂度为O(N^2),提交代码后会报错Time Limit Exceeded。时间超限。

那么另寻他路,我们知道哈希表查找的时间复杂度是O(1),这里可以借助哈希表查找可以将时间复杂度降低到O(n)。

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int ,int> map;
        vector<int> result;
        for(int i = 0 ; i < nums.size() ; ++i)
        {
            if(map.find(target - nums[i])!=map.end())
            {
                if(map[target - nums[i]]!=i)
                {
                    result.push_back(map[target - nums[i]]);
                    result.push_back(i);
                    return result;
                }
            }
            map[nums[i]] = i;
        }
    }
};

【一天一道LeetCode】 #1 Two Sum的更多相关文章

  1. 【一天一道LeetCode】#371. Sum of Two Integers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...

  2. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  3. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【一天一道LeetCode】#112. Path Sum

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【一天一道LeetCode】#64. Minimum Path Sum.md

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  7. 【一天一道LeetCode】#39. Combination Sum

    一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...

  8. LeetCode #303. Range Sum Query

    问题: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  9. 【一天一道LeetCode】#172. Factorial Trailing Zeroes

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. Zookeeper的安装配置及基本开发

    一.简介 Zookeeper 是分布式服务框架,主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集群管理.分布式应用配置项的管理等等. ZooKeeper的目标就 ...

  2. 安卓框架——SlidingMenu使用技巧

    SlidingMenu的一些常用属性 原文转载http://blog.csdn.net/zwl5670/article/details/48274109 [java] view plain copy ...

  3. Apache shiro集群实现 (八) web集群时session同步的3种方法

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  4. C++语言编译系统提供的内部数据类型的自动隐式转换

    C++语言编译系统提供的内部数据类型的自动隐式转换规则如下: 程序在执行算术运算时,低类型自动隐式转换为高类型. 在函数调用时,将实参值赋给形参,系统隐式的将实参转换为形参的类型,并赋值给形参. 函数 ...

  5. SQL LOADER使用

    转自huan.gu专栏:http://blog.csdn.net/gh320/article/details/17048907 1.执行的命令 sqlldr 数据库用户名/密码 control=控制文 ...

  6. 使用shell操作HDFS

    前提是都已经配置好了,可以参考hadoop伪分布安装:http://blog.csdn.net/jerome_s/article/details/25788967 linux的文件系统与hdfs的关系 ...

  7. 自制Linux重命名命令

    相比于Windows上的ren命名,Linux还真的是没有一个特定的重命名的命令.(虽然可以间接的使用mv来实现).下面我就来自己写一个简单的重命名命令. 准备工作 操作系统: Linux内核的系统都 ...

  8. FFmpeg源代码简单分析:avformat_open_input()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  9. XML命名规则

    XML = Extensible Markup Language,可扩展标记语言 XML 标签对大小写敏感,XML 标签对大小写敏感.在XML 中,标签 <Letter> 与标签 < ...

  10. 【Unity Shaders】法线纹理(Normal Mapping)的实现细节

    写在前面 写这篇的目的是为了总结我长期以来的混乱.虽然题目是"法线纹理的实现细节",但其实我想讲的是如何在shader中编程正确使用法线进行光照计算.这里面最让人头大的就是各种矩阵 ...