C++:

vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hashMap;

for (int i = ; i < nums.size(); i++) {
if(hashMap.find(nums[i]) == hashMap.end()){
hashMap[target-nums[i]] = i;
}else{
return vector<int> {hashMap[nums[i]]+, i+};
}
}
return vector<int> {};
}

1. hashMap[value] = i 使得value + nums[i] = target

2. unordered_map其内部存储为hash、遍历无序、使用需重载operator ==以及hash_value(), map存储为树、需重载operator <; 详见文章http://blog.csdn.net/orzlzro/article/details/7099231

3. hashMap[nums[i]]一定比i小,因前者值为几个迭代之前的i而这里i从小到大

Python:

def twoSum(self, nums, target):
m_map = {}
for i in range(len(nums)):
if nums[i] not in m_map:
m_map[target - nums[i]] = i
else:
return[m_map[nums[i]]+1, i+1]

讨论里有更简洁代码,

for j, item in enumerate(nums, 1): #start from 1 & items are entries
  i = m_map.get(item, -1) #the same as m_map[] but instead of crush, gives back -1 when couldn't find item
if i > 0:
  return [i, j]
m_map[target - item] = j

LeetCode 1. twoSums的更多相关文章

  1. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  2. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  3. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  4. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  8. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  9. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

随机推荐

  1. mysql insert和前台显示乱码

    近期在搞服务端.遇到问题例如以下, 在mysql中插入中文乱码.或mysql中中文正常显示,但jsp在前台显示mysql中的中文时乱码. 解决方法,进入mysql控制台,运行 SET characte ...

  2. 使用Vitamio打造自己的Android万能播放器(1)——准备

    前言 虽然Android已经内置了VideoView组件和MediaPlayer类来支持开发视频播放器,但支持格式.性能等各方面都十分有限,这里与大家一起利用免费的Vitamio来打造属于自己的And ...

  3. data按钮

    1.加载状态 通过按钮(Button)插件,您可以添加进一些交互,比如控制按钮状态,或者为其他组件(如工具栏)创建按钮组. 如需向按钮添加加载状态,只需要简单地向 button 元素添加 data-l ...

  4. Linux下Oracle常见安装错误[Z]

    #./runInstaller之后出现如下的错误信息: RedHat AS5 x86上安装Oracle1020 Exception in thread "main" java.la ...

  5. User cannot be resolved to a type

    出现 User cannot be resolved to a type 不知道具体问题出在哪里但是我经过将全路径输入并保存后错误消失 将User选上,然后点击保存就可以了! 最后我发现错误消失了!

  6. C语言处理CSV文件的方法(二)

    strtok函数的使用是一个老生常谈的问题了.该函数的作用很大,争议也很大.以下的表述可能与一些资料有区别或者说与你原来的认识有差异,因此,我尽量以实验为证.交代一下实验环境是必要的,win7+vc6 ...

  7. leetcode Binary Tree Paths python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  8. Firefox中打开DOM Inspector

    一: 二:ctrl+shift+I

  9. MongoDB 从0开始

    MongoDB https://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/ 在Mac上安装 brew update brew i ...

  10. 9.java.lang.ClassCastException

    java.lang.ClassCastException 数据类型转换异常 当试图将对某个对象强制执行向下转型,但该对象又不可转换又不可转换为其子类的实例时将引发该异常,如下列代码. Object o ...