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 ...
随机推荐
- deal with 'non-admin area' warn
We usually use the follow code to delete product in Magento $product = Mage::getSingleton('catalog/p ...
- ACM/ICPC2014鞍山现场赛E hdu5074Hatsune Miku
题目链接:pid=5074">http://acm.hdu.edu.cn/showproblem.php?pid=5074 题意: 给定一个m*m的矩阵mp.然后给定一个长度为n的序列 ...
- Quartz.NET 2.0 作业调度框架使用
Quartz.NET是一个开源的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不 ...
- 使用关联对象(AssociatedObject)为UIButton添加Block响应
在开发中,要给UIButton添加点击事件的话,通常的做法是这样的 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [ ...
- JDBC连接数据库及增删改查操作
什么是JDBC?Java语言访问数据库的一种规范,是一套APIJDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Java语言中的接口和类 ...
- 20151205--JDBC-2
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 《C++ Primer Plus 6th》读书笔记 - 第8章 函数探幽
1. 摘录 默认参数指的是当函数调用中省略了实参时自动使用的一个值. 默认参数并非编程方面的重大突破,而只是提供了一种便捷的方式.使用默认参数,可以减少要定义的析构函数.方法以及方法重载的数量. 试图 ...
- C-JAVA 论坛
http://www.cnblogs.com/lpjia/ JAVA黑马 http://java.itheima.com/java/service/javacourse.shtml
- NodeJS爬虫系统初探
NodeJS爬虫系统 NodeJS爬虫系统 0. 概论 爬虫是一种自动获取网页内容的程序.是搜索引擎的重要组成部分,因此搜索引擎优化很大程度上是针对爬虫而做出的优化. robots.txt是一个文本文 ...
- HTML5 canvas入门
HTML5 Canvas入门 <canvas> 标签定义图形,比如图表和其他图像,您必须使用脚本来绘制图形.在画布上(Canvas)画一个红色矩形,渐变矩形,彩色矩形,和一些彩色的文字. ...