要点:这题是combination的应用,从左向右想比从右向左容易。

  • 因为有结果从小到大的要求,暗示用combintion而不是permutation
  • 其实就是从小到大验证因子,每个因子和其对称因子立即成为一个res到solutions里
  • 因子的范围是i*i<=n,因为对称的部分已经在验证当前因子的时候push到solutions里了
  • 每层的循环内从当前因子开始验证,每层要分解的数是上一层分解后剩下的

错误点:

  • confusion:每一个因子都有可能,所以是combination
  • 与combination稍有不同,每次的push的res是对称的两个因子,而传到下一层的是当前因子。

https://repl.it/CfT5/1 (recursion)

https://repl.it/Cf13/1 (iteration)

  • 就是把中间状态存到queue or stack里,然后从里面取出来继续,order对应结果的order,所以无所谓
  • 错误点:别忘了n%i的条件
class Solution(object):
def getFactors(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
def helper(n, start, res, solutions):
while start*start<=n: # error: <=n, not <n
if n%start==0:
solutions.append(res+[start, n/start])
helper(n/start, start, res+[start], solutions)
start+=1 solutions = []
helper(n, 2, [], solutions)
return solutions sol = Solution()
assert sol.getFactors(12)==[[2,6],[2,2,3],[3,4]]
assert sol.getFactors(4)==[[2,2]]
from collections import deque

class Solution(object):
def getFactors(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
q = deque([(n, 2, [])])
solutions = []
while q:
n, start, res = q.popleft()
while start*start<=n:
if n%start==0:
solutions.append(res+[start, n/start])
q.append((n/start, start, res+[start]))
start+=1 return solutions

边工作边刷题:70天一遍leetcode: day 84-2的更多相关文章

  1. 边工作边刷题:70天一遍leetcode: day 84

    Flatten 2D Vector 要点: 这题是2d的iterator,一般对于1d的情况,hasNext()是不需要做移动的.而2d不同,core iterator是j向的,而i向要在hasNex ...

  2. 边工作边刷题:70天一遍leetcode: day 89

    Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...

  3. 边工作边刷题:70天一遍leetcode: day 77

    Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...

  4. 边工作边刷题:70天一遍leetcode: day 78

    Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...

  5. 边工作边刷题:70天一遍leetcode: day 85-3

    Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...

  6. 边工作边刷题:70天一遍leetcode: day 101

    dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...

  7. 边工作边刷题:70天一遍leetcode: day 1

    (今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...

  8. 边工作边刷题:70天一遍leetcode: day 70

    Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...

  9. 边工作边刷题:70天一遍leetcode: day 71-3

    Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...

  10. 边工作边刷题:70天一遍leetcode: day 71-2

    One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...

随机推荐

  1. Linux守护进程实现程序只运行一次

    1.守护进程 守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件. 2.让程序只运行一次 如果让程序只运行一次,有很多方法,此处的一种 ...

  2. 线段树的区间更新---A Simple Problem with Integers

    POJ   3468 Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 100 ...

  3. mysql awr v1.0.2发布

    改进说明: 1.新增监控Mysql实例通过web控制台http://ip:port/hosts.html进行,无需更改mysql-jdbc.properties配置文件和spring-servlet. ...

  4. RadioButton 自定义控件

    在res/drawable新建radiobutton.xml(本案例为video——evaluate.xml)如下 <?xml version="1.0" encoding= ...

  5. Webform(分页、组合查询)

    一.分页 1.写查询方法: public List<Student> Select(int PageCount, int PageNumber) {//PageCount为每页显示条数,P ...

  6. jQuery Mobile笔记

    1.获取jQuery mobile 文件,访问jQuerymobile网站下载 (貌似使用jquery mobile后,jquery会自动在网页中添加一些class类,第一次知道的我是被吓呆的!!) ...

  7. SharePoint 2013 自定义模板页后在列表里修改不了视图

    前言 最近系统从2010升级至2013,有自定义模板页.突然发现在列表中切换不了视图,让我很费解. 我尝试过以下解决方案: 去掉自定义css 去掉自定义js 禁用所有自定义功能 结果都没有效还是一样的 ...

  8. Atitit.图片木马的原理与防范 attilax 总结

    Atitit.图片木马的原理与防范 attilax 总结 1.1. 像图片的木马桌面程序1 1.2. Web 服务端图片木马1 1.3. 利用了Windows的漏洞1 1.4. 这些漏洞不止Windo ...

  9. Add a file to a Document Library and update metadata properties in a single method添加文档的方法

    private void AddFileToDocumentLibrary(string documentLibraryUrl, string filename, byte[] file_bytes, ...

  10. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q127-Q130)

    Question 127You create a custom list named Products.You need to perform a Representational State Tra ...