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 ...
随机推荐
- jquery实现隐藏显示层动画效果、仿新浪字符动态输入、tab效果
已经有两年多没登陆csdn账号了,中间做了些旁的事,可是现在却还是回归程序,但改做前端了,虽然很多东西都已忘得差不多了,但还是应该摆正心态,慢慢来,在前端漫游,做一只快乐双鱼. 路是一步一步走出来的, ...
- 一个tabBarController管理多个Storyboard
随着项目的业务逻辑越来越复杂,随着项目越来越大,那么我们Storybard中得控制器就越来越多, 就越来越难以维护.然而使用Storyborad又能更方便的帮助我们做屏幕适配(PS:尤其在6.6+出来 ...
- oracle10G之前介质下载地址【珍藏版】
今天在互联网搜了一下相关介质下载,自己记录一下. Oracle9i Database Release 2 Enterprise/Standard/Personal Edition for Window ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
- iOS 自定义button
UIButton默认的布局是左侧image,右侧title,如果想要改变image与title的frame,直接设置是不会有效果的.可以通过titleEdgeInsets.imageEdgeInset ...
- js——cookie
cookie:存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来向访问者电脑上存储数据 1.不同的浏览器存放的cookie位置不一样,也是不能通用的 2. cookie的存储是 ...
- scanf 和cin 的区别
笔试的时候经常遇到突然string s;cin>>s; 有的时候编译会错误,不知道为什么. 今天在练习枚举类型的时候,也遇到这样一个问题. enum weekday{Monday,Tues ...
- SSH整合,applicationContext.xml中配置hibernate映射文件问题
今天在applicationContext.xml中配置sessionFactory时遇到了各种头疼的问题,现在总结一下: 1.<property name="mappingDirec ...
- struts2 + jquery 开发环境下的ajax构建方法(action写法 + struts.xml配置 + js调用代码)
1.action写法 public class RegisterAction extends ActionSupport { private InputStream inputStream; /** ...
- Android Spinner 下拉列表
private Spinner spinner ; private List<String> list ; private ArrayAdapter< ...