边工作边刷题:70天一遍leetcode: day 71-3
Two Sum I/II/III
要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1
https://repl.it/CrZG
错误点:
- TLE:defaultdict is slower than normal dict
- TLE:.keys() copies keys, it’s way slower
# Design and implement a TwoSum class. It should support the following operations: add and find.
# add - Add the number to an internal data structure.
# find - Find if there exists any pair of numbers which sum is equal to the value.
# For example,
# add(1); add(3); add(5);
# find(4) -> true
# find(7) -> false
# Hide Company Tags LinkedIn
# Hide Tags Hash Table Design
# Hide Similar Problems (E) Two Sum (E) Unique Word Abbreviation
from collections import defaultdict
class TwoSum(object):
def __init__(self):
"""
initialize your data structure here
"""
self.counts = defaultdict(int)
def add(self, number):
"""
Add the number to an internal data structure.
:rtype: nothing
"""
self.counts[number]+=1
def find(self, value):
"""
Find if there exists any pair of numbers which sum is equal to the value.
:type value: int
:rtype: bool
"""
for k in self.counts.keys():
if value-k not in self.counts or (k==value-k and self.counts[k]==1):
continue
return True
return False
# Your TwoSum object will be instantiated and called as such:
# twoSum = TwoSum()
# twoSum.add(number)
# twoSum.find(value)
边工作边刷题:70天一遍leetcode: day 71-3的更多相关文章
- 边工作边刷题:70天一遍leetcode: day 71
Longest Substring with At Most Two Distinct Characters # Given a string, find the length of the long ...
- 边工作边刷题:70天一遍leetcode: day 89
Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- 边工作边刷题:70天一遍leetcode: day 101
dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
- 边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...
随机推荐
- scons学习
http://blog.csdn.net/andyelvis/article/category/948141 http://www.scons.org/doc/production/HTML/scon ...
- transfer between javabean and map
1. java bean 转化成 map import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.P ...
- c#反射获取常量属性名以及其值(真正可用)
最近因为要开发rpc平台的c#客户端,其中部分常量类为了自动加载的map,需要反射解析出静态常量,往上搜了一堆,都各种的不靠谱. 亲自研究了下,如下: Type t = typeof(SpiderEr ...
- Android的新虚拟机ART
- Visual Studio添加dll程序集引用操作步骤
Visual Studio 中添加引用的操作: 在“解决方案资源管理器”中,先右击项目图标,在弹出菜单选择“添加引用...” 然后在弹出的窗口中选择所要添加的选项,点击确定就可以了. 原文:http: ...
- 【读书笔记】iOS-GCD-Dispatch Source
一,Dispatch Source是BSD系内核惯有功能kqueue的包装. 参考资料:<Objective-C高级编程 iOS与OS X多线程和内存管理>
- ReactiveCocoa之UI篇
前言: 上一篇讲ReactiveCocoa是函数响应式编程,并将多种事件响应的方式统一起来,使得不同的事件响应方式高度统一.同时也讲了ReactiveCocoa框架里面常见的几个概念.接下来基于那几个 ...
- Objective-C之Block
Block基本概念 本小节知识点: [了解]什么是Block [理解]block的格式 1.什么是Block Block是iOS中一种比较特殊的数据类型 Block是苹果官方特别推荐使用的数据类型, ...
- UVa 103 - Stacking Boxes(dp求解)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- andorid开发eclipse常见问题
1.报错: BUILD FAILED D:\workspace\ganji\build.xml:144: The following error occurred while executing th ...