1. Two 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:暴力的一种方法,将nums的所有两个数字都尝试一遍
两个for循环,时间复杂度为O(n^2),空间复杂度为O(1). 需要复习的知识:vector的使用:
定义一个vector类: vector<int> result;
定义一个返回vector的函数: vector<int> twoSum(vector<int>& nums,int target)
以及vector类中包含的数据的个数: nums.size() 记得括号不能丢
vector在尾部再加入一个数据: result.push_back(i); 代码部分:
class Solution{
public:
    vector<int> twoSum(vector<int>& nums,int target)
    {
      vector<int> result;
      for(int i=0;i<nums.size();i++)//nums.size()一定记得要带括号哦
      {
        for(int j=i+1;j<nums.size();j++)//important to be i+1 not i
        {
          if(nums[i]+nums[j]==target)
          {
            result.push_back(i);
            result.push_back(j);
            return result;
          }
        }
      }
    }
};
思路2:使用哈希表的解法
其实刚刚的思路可以转换为,我们选择了一个数字以后,下一个选择的数字就必须是target减去这个数字。
刚刚的思路其实就是通过再次的遍历来寻找这个数字。而这个寻找的过程可以简化。
简化的方式就是使用哈希表(让在数组的每个元素对应一个key然后就可以查询)。
通过使用这一次哈希就一下子让再次寻找的时间复杂度从O(n)降到了O(1)。(当然,这是在哈希表没有发生冲突的情况下)
而我们可以设计这样一个O(1)的哈希表。 需要复习的知识:
需要准备的知识主要就是怎么创建一个比较合适的哈希表,网上使用比较多的就是unordered_map
用法:
[查找元素是否存在]
    若有unordered_map<int, int> mp;查找x是否在map中
    方法1:  若存在  mp.find(x)!=mp.end()
    方法2:  若存在  mp.count(x)!=0
[插入数据]
    map.insert(Map::value_type(1,"Raoul"));
[遍历map]
    unordered_map<key,T>::iterator it;
    (*it).first;        //the key value
    (*it).second   //the mapped value
    for(unordered_map<key,T>::iterator iter=mp.begin();iter!=mp.end();iter++)
          cout<<"key value is"<<iter->first<<" the mapped value is "<< iter->second; 代码部分:
class Solution{
public:
        vector<int> twoSum(vector<int>& nums,int target)
        {
            vector<int> result;
            unordered_map<int,int> uMap;
            int res=0;
            for(int i=0;i<nums.size();i++)
            {
                res=target-nums[i];
                unordered_map<int,int>::iterator it=uMap.find(res);
                if(it!=uMap.end())
                {
                    result.push_back(it->second);
                    result.push_back(i);
                    return result;
                }
                uMap[nums[i]]=i;
            }
        }
};

leetcode题解 1.TwoSum的更多相关文章

  1. [LeetCode 题解]: Two Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

  2. leetcode题解(持续更新)

    leetcode题解 1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  3. LeetCode题解——Two Sum

    题目地址:https://oj.leetcode.com/problems/two-sum/ Two Sum Given an array of integers, find two numbers ...

  4. [LeetCode]题解(python):001-Two-Sum

    题目来源: https://leetcode.com/problems/two-sum/ 题意分析: 这道题目是输入一个数组和target,要在一个数组中找到两个数字,其和为target,从小到大输出 ...

  5. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  6. leetcode题解-122买卖股票的最佳时期

    题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...

  7. 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)

    目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...

  8. 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

  9. 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)

    目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...

随机推荐

  1. Solution about MB STAR C4, MB STAR C5 Update and can not test vehicles problems

    Solution about MB Star C4, MB Star C5 Update and can not test vehicles problems 1. Make sure your co ...

  2. java学习路线之必会的java基础教程

    大数据产业已进入发展的“快车道”,急需大量优秀的大数据人才作为后盾.如果你是Java编程出身,那学习大数据自然是锦上添花:但如果你是刚刚接触大数据技术,还在Java编程基础阶段,这篇文章非常值得你看! ...

  3. POJ 1038 Bugs Integrated, Inc.(DFS + 三进制状压 + 滚动数组 思维)题解

    题意:n*m方格,有些格子有黑点,问你最多裁处几张2 * 3(3 * 2)的无黑点格子. 思路:我们放置2 * 3格子时可以把状态压缩到三进制: 关于状压:POJ-1038 Bugs Integrat ...

  4. POJ2955--Brackets 区间DP入门 括号匹配

    题意很简单,就是求给出串中最大的括号匹配数目.基础题,格式基本为简单区间dp模板. #include<iostream> #include<string.h> using na ...

  5. 面试必问的SpringCloud实现原理图

    引言 面试中面试官喜欢问组件的实现原理,尤其是常用技术,我们平时使用了SpringCloud还需要了解它的实现原理,这样不仅起到举一反三的作用,还能帮助轻松应对各种问题及有针对的进行扩展. 以下是 课 ...

  6. 【BZOJ】 4813: [Cqoi2017]小Q的棋盘

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4813 暴力转移就好,考虑以某一个点为根的子树分为是否走回来两种情况 ${f_{i,j}}$ ...

  7. C#以太坊基础入门

    在这一部分,我们将使用C#开发一个最简单的.Net控制台应用,来接入以太坊节点,并打印 所连接节点旳版本信息.通过这一部分的学习,你将掌握以下技能: 如何使用节点仿真器 如何在命令行访问以太坊节点 如 ...

  8. robot framework学习二-----元素定位

    文章摘自:https://www.cnblogs.com/fnng/p/3901391.html 不要误认为Robot framework 只是个web UI测试工具,更正确的理解Robot fram ...

  9. C#时间戳的简单实现

    Introduction: 在项目开发中,我们都经常会用到时间戳来进行时间的存储和传递,最常用的Unix时间戳(TimeStamp)是指格林尼治时间1970年1月1日0时(北京时间1970年1月1日8 ...

  10. OpenStack笔记

    *********virsh xml文件解读****************************** https://libvirt.org/format.html https://libvirt ...