LeetCode 1. twoSums
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的更多相关文章
- 【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 ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
随机推荐
- poj 2728 Desert King(最优比例生成树)
#include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #i ...
- Body joints angle using Kinect
http://stackoverflow.com/questions/12608734/body-joints-angle-using-kinect-checking-time-interval?rq ...
- Android dump .so 文件crash log
众所周知,在android系统上,有时候我们遇到so文件的crash仅仅能打log,可是非常多时候并不知道crash在什么地方,幸运的是crash后,一般能够产生一个.dmp文件. 我们能够依据这个文 ...
- 浏览器返回按钮不会触发onLoad事件
最近在做一个移动端项目,发现移动端某些返回和PC端是有差异的, 比如ios中返回按钮是直接使用缓存的, 不会执行任何js代码, 这个问题很蛋疼, 例如, 在提交的时候将按钮设置为loading状态, ...
- 相比于python2.6,python3.0的新特性。
这篇文章主要介绍了相比于python2.6,python3.0的新特性.更详细的介绍请参见python3.0的文档. Common Stumbling Blocks 本段简单的列出容易使人出错的变动. ...
- Windows 7/Vista 开机自动登录
“Win”+“R”打开命令窗口,输入"control userpasswords2"(不包括引号),或者 “rundll32 netplwiz.dll,UsersRunDll” 回 ...
- Python进阶之闭包
闭包 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB","S ...
- Android studio mac版本快捷键
Mac下快捷键的符号所对应的按键 ⌥—> option|alt ⇧—>shift ⌃—>control ⌘—>command ⎋—>esc 注: 与F6/F7/F12等F ...
- 我来讲讲在c#中怎么进行xml文件操作吧,主要是讲解增删改查!
我把我写的四种方法代码贴上来吧,照着写没啥问题. 注: <bookstore> <book> <Id>1</Id> <tate>2010-1 ...
- jmeter实例演示
Jmeter 是比较轻便的性能测试工具,下面根据一个实例演示下jmeter的常见用法 一.前期准备,在使用之前,先分析测试需求,比如:需不要登录?需不需要监视服务器性能?多线程还是多循环?需不需根据流 ...