摘要:
https://oj.leetcode.com/problems/linked-list-cycle/判断一个链表是否为循环链表(这个链表可能是 1 2 3 4 然后4指向2)巧妙的方法:设置两个指针,一个slow,一个fast。每次slow走一个,fast走两个,如果是循环链表,它俩有相等的时候。...
阅读全文
posted @ 2014-07-02 19:54
qingcheng奕
阅读(198)
推荐(0)
摘要:
https://oj.leetcode.com/problems/edit-distance/动态规划,它的小规模问题是:当 word1 word2都比较小的时候,word1变成word2需要的变化数。设数组 record[i][j] 表示word1 从0到 i ,word2 从 0 到 j 的 w...
阅读全文
posted @ 2014-07-02 18:04
qingcheng奕
阅读(249)
推荐(0)
摘要:
https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/二叉树的处理,将二叉树转换成类似一个单链表的东东,并且原地。类似于先跟遍历的顺序。定义函数TreeNode* subflatten(TreeNode *root)并...
阅读全文
posted @ 2014-07-02 10:02
qingcheng奕
阅读(183)
推荐(0)
摘要:
https://oj.leetcode.com/problems/insertion-sort-list/链表实现插入排序首先插入排序是指:a b c d e g m 对b也就是第二个位置选做元素num开始遍历n-1次对每个num,找到从第0个位置开始,它应该待的位置,把它插入进去。对于链表来说,首...
阅读全文
posted @ 2014-07-01 16:20
qingcheng奕
阅读(210)
推荐(0)
摘要:
https://oj.leetcode.com/problems/generate-parentheses/输入n产生n个( ,n个 )组成的所有合法的括号组合。现在纸上画画,找到规律:1.每一个位置上 ( 的个数必须 >= ) 的个数2.如果 ( 的个数是n了,则只能再画 ) 了3.否则,既可以是...
阅读全文
posted @ 2014-06-30 21:33
qingcheng奕
阅读(162)
推荐(0)
摘要:
https://oj.leetcode.com/problems/first-missing-positive/给一列数,找出缺失的第一个正数。要求时间复杂度 O(n)第一步遍历一遍,找出最大的数和最小的数。第二步建立一个vector,以 max+1 为size。第三部遍历一遍,存储每个存在的数到相...
阅读全文
posted @ 2014-06-30 21:07
qingcheng奕
阅读(160)
推荐(0)
摘要:
https://oj.leetcode.com/problems/divide-two-integers/在不使用乘法、除法、求余的情况下计算除法。使用减法计算,看看减几次。刚开始寻思朴素的暴力做,然后超时了。于是开始增大每次的被减数但是溢出了。2的32次方=4294967296(无符号),带符号再...
阅读全文
posted @ 2014-06-30 20:36
qingcheng奕
阅读(214)
推荐(0)
摘要:
https://oj.leetcode.com/problems/distinct-subsequences/对于string S 和 T求,T 是 S的几种子串。首先想到了递归方法,列出递归公式,奈何超时了:如果S[sBegin] == T[tBegin] 则是 numSubDistinct(sB...
阅读全文
posted @ 2014-06-30 19:22
qingcheng奕
阅读(160)
推荐(0)
摘要:
https://oj.leetcode.com/problems/decode-ways/对于1 3 2 4 3 1 可以一次选择一个数,或者一次选择两个数进行拆分使用递推来做,但是有几个情况关于0的,要考虑清楚了。1. A[0] == '0' return 02. A[1] = 13. A[1] ...
阅读全文
posted @ 2014-06-30 09:51
qingcheng奕
阅读(274)
推荐(0)
摘要:
https://oj.leetcode.com/problems/count-and-say/求经过n次变换后,变成了什么。1 11 21 1211 111221ps. 3 变成 ‘3’,为 3 + '0'class Solution {public: string countAndSay(...
阅读全文
posted @ 2014-06-29 19:24
qingcheng奕
阅读(171)
推荐(0)