随笔分类 -  leetcode

摘要:```python class Solution(object): def findComplement(self, num): """ :type num: int :rtype: int """ res=0 i=0 while num>0: if nu... 阅读全文
posted @ 2017-03-29 23:21 clq.lib 阅读(154) 评论(0) 推荐(0)
摘要:"Missing Number" Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, Gi 阅读全文
posted @ 2016-07-05 17:56 clq.lib 阅读(175) 评论(0) 推荐(0)
摘要:题目 题目: "CoinChange" 有面额不等的coins,数量无限,要求以最少的$coins$凑齐所需要的$amount$。 若能,返回所需的最少coins的数量,若不能,返回 1。 Example 1: coins = [1, 2, 5], amount = 11 return 3 (11 阅读全文
posted @ 2016-01-30 15:41 clq.lib 阅读(275) 评论(0) 推荐(0)
摘要:leetcode上的题目 给出一段只包含 、`' ' ' ' ()`),求所有可能的结果。 例子: Input: "2 3 4 5" (2\ (3 (4\ 5))) = 34 ((2\ 3) (4\ 5)) = 14 ((2\ (3 4))\ 5) = 10 (2\ ((3 4)\ 5)) = 1... 阅读全文
posted @ 2015-08-12 10:19 clq.lib 阅读(280) 评论(0) 推荐(0)
摘要:对链表进行排序,要求时间复杂度为O(n log n) ,不使用额外的空间。我一开始的想法是借助quicksort的思想,代码如下:# time O(nlog(n))# Definition for singly-linked list.class ListNode: def _... 阅读全文
posted @ 2015-07-10 15:35 clq.lib 阅读(170) 评论(0) 推荐(0)
摘要:DP对于第i个状态(房子),有两种选择:偷(rob)、不偷(not rob)递推公式为: f(i)=max⎧⎩⎨⎪⎪{f(i−1)+vali,f(i−2)+vali,robi−1==0robi−1==1f(i−1),robnot rob f(i)为动态规划表(DP表)(状态i下的最... 阅读全文
posted @ 2015-07-09 20:53 clq.lib 阅读(195) 评论(0) 推荐(0)
摘要:反转链表:比较简单的问题,可以遍历也可以递归。# Definition for singly-linked list.class ListNode: def __init__(self, x): self.val = x self.next = No... 阅读全文
posted @ 2015-07-09 18:44 clq.lib 阅读(163) 评论(0) 推荐(0)
摘要:最先想到的是把两个linked lists 合并成一个。 这样从第一个开始一个一个吞并,直到所有list都被合并。class ListNode:# Definition for singly-linked list. def __init__(self, x): ... 阅读全文
posted @ 2015-07-09 17:43 clq.lib 阅读(349) 评论(0) 推荐(0)