Fork me on GitHub

随笔分类 -  leetcode

上一页 1 2 3 4 5 6 ··· 9 下一页
摘要:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Input: [3,0,1] Output 阅读全文
posted @ 2018-05-24 20:22 ranjiewen 阅读(156) 评论(0) 推荐(0) 编辑
摘要:蓄水池抽样——《编程珠玑》读书笔记 382. Linked List Random Node 398. Random Pick Index 从n个数中随机选取m个 等概率随机函数面试题总结 蓄水池抽样——《编程珠玑》读书笔记 382. Linked List Random Node 398. Ran 阅读全文
posted @ 2018-04-30 12:05 ranjiewen 阅读(1334) 评论(0) 推荐(0) 编辑
摘要:236. Lowest Common Ancestor of a Binary Tree 题目 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According t 阅读全文
posted @ 2018-04-26 17:33 ranjiewen 阅读(204) 评论(0) 推荐(0) 编辑
摘要:97. Interleaving String 题目 解析 "LeetCode(97) Interleaving String" 状态cache[i][j]表示,s1的前i个字符和s2的前j个字符是否能交叉构成s3的前i+j个字符 初始化: cache[0][0] = True 因为两个空字符串可以 阅读全文
posted @ 2018-04-14 21:52 ranjiewen 阅读(205) 评论(0) 推荐(0) 编辑
摘要:93. Restore IP Addresses 题目 解析 三重循环,遍历三个小数点的位置,对每个位置check一下即可:【LeetCode】93. Restore IP Addresses](https://www.cnblogs.com/ganganloveu/p/3780607.html) 阅读全文
posted @ 2018-04-14 21:15 ranjiewen 阅读(150) 评论(0) 推荐(0) 编辑
摘要:96. Unique Binary Search Trees 题目 解析 "LeetCode96:Unique Binary Search Trees" 给定一个序列1.....n,为了构造所有二叉树,我们可以使用1......n中的每一个数i作为根节点,自然1......(i 1)必然位于树的左子 阅读全文
posted @ 2018-04-14 19:28 ranjiewen 阅读(153) 评论(0) 推荐(0) 编辑
摘要:98. Validate Binary Search Tree 题目 解析 需要注意的是,左子树的所有节点都要比根节点小,而非只是其左孩子比其小,右子树同样。这是很容易出错的一点是,很多人往往只考虑了每个根节点比其左孩子大比其右孩子小。如下面非二分查找树,如果只比较节点和其左右孩子的关系大小,它是满 阅读全文
posted @ 2018-04-14 15:07 ranjiewen 阅读(194) 评论(0) 推荐(0) 编辑
摘要:99. Recover Binary Search Tree 题目 解析 一种非递归且不使用栈,空间复杂度为O(1)的遍历方法 "[LeetCode] Recover Binary Search Tree 复原二叉搜索树" 题目来源 "99. Recover Binary Search Tree" 阅读全文
posted @ 2018-04-14 14:24 ranjiewen 阅读(163) 评论(0) 推荐(0) 编辑
摘要:92. Reverse Linked List II 题目 解析 1.通过构建新节点,避免从头开始反转讨论 2.技巧就是向一个节点前面插入节点,指针每一次向前移动 题目来源 "92. Reverse Linked List II" 阅读全文
posted @ 2018-04-12 21:56 ranjiewen 阅读(134) 评论(0) 推荐(0) 编辑
摘要:90. Subsets II 题目 解析 "[LeetCode] Subsets II 子集合之二 " 使用非递归迭代和递归实现 从前往后遍历,保留下当前已经计算好的组合集合。对当前i号元素的加入,就是有i和没有i的场景。没有i的场景就是已有的集合。有i的就是对已有的集合追加上i后的集合。两个的并集 阅读全文
posted @ 2018-04-12 20:51 ranjiewen 阅读(170) 评论(0) 推荐(0) 编辑
摘要:91. Decode Ways 题目 解析 主要理解题意,dp[i]取决于dp[i 1]和dp[i 2];然后dp[0]的初始化 C++ // 91. Decode Ways class Solution_91 { public: // 限制条件,比如说一位数时不能为0,两位数不能大于26,其十位上 阅读全文
posted @ 2018-04-12 15:21 ranjiewen 阅读(176) 评论(0) 推荐(0) 编辑
摘要:LeetCode 89. Gray Code 题目 解析 这道题的要求是按顺序生成所有n位格雷码。 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code),另外由于最大数与最小数之间也仅一位数不同,即“首尾相连”,因此又称循环码或反射码。在数字系统中, 阅读全文
posted @ 2018-04-11 09:57 ranjiewen 阅读(244) 评论(0) 推荐(0) 编辑
摘要:88. Merge Sorted Array 题目 解析 最优解:从后往前处理,不需要开辟额外空间 题目来源 "88. Merge Sorted Array" 阅读全文
posted @ 2018-04-11 09:26 ranjiewen 阅读(172) 评论(0) 推荐(0) 编辑
摘要:87. Scramble String 题目 解析 "【LeetCode】87. Scramble String解法及注释" 题意在于判断一个字符串是否为另一个字符串“乱序”得到,这种乱序采用的方式是将一个字符串从某个位置“割开”,形成两个子串,然后对两个子串进行同样的“割开”操作,直到到达叶子节点 阅读全文
posted @ 2018-04-10 21:41 ranjiewen 阅读(165) 评论(0) 推荐(0) 编辑
摘要:86. Partition List 题目 解析 思路:新建两个节点preHead1与preHead2,分别为指向两个链表的头结点。把节点值小于x的节点链接到链表1上,节点值大等于x的节点链接到链表2上。最后把两个链表相连即可 right.next=null;//这句很重要!链表最后一个元素如果小于 阅读全文
posted @ 2018-04-10 20:52 ranjiewen 阅读(158) 评论(0) 推荐(0) 编辑
摘要:85. Maximal Rectangle 题目 解析 "[LeetCode] Maximal Rectangle 最大矩形 " C++ // 85. Maximal Rectangle class Solution_85 { public: int largestRectangleArea(vec 阅读全文
posted @ 2018-04-10 20:19 ranjiewen 阅读(158) 评论(0) 推荐(0) 编辑
摘要:84. Largest Rectangle in Histogram 题目 解析 直接的暴力的思路就是对于每一组子数组,找到其中最低的高度,然后求面积,进而求出最大的矩形面积。总共有n^2个子数组,找最低高度是O(n)的操作,所以复杂度是O(n^3)。 进一步,可以从每一个bar往两边走,以自己的高 阅读全文
posted @ 2018-04-10 19:52 ranjiewen 阅读(171) 评论(0) 推荐(0) 编辑
摘要:题目 83. Remove Duplicates from Sorted List 解析 "[LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项" 82. Remove Duplicates from Sorted List II 解析 阅读全文
posted @ 2018-04-08 14:40 ranjiewen 阅读(379) 评论(0) 推荐(0) 编辑
摘要:81. Search in Rotated Sorted Array II 题目 解析 和Search in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现。不过正是因为这个条件的出现,出现了比较复杂的case,甚至影响到了算法的时间复杂度。原来我们是依靠中间和 阅读全文
posted @ 2018-04-07 21:10 ranjiewen 阅读(143) 评论(0) 推荐(0) 编辑
摘要:80. Remove Duplicates from Sorted Array II 题目 解析 "参考ref nowcoder" 方法一:很灵活的方法,扩展性较强,如果将occur0&&i& nums) { if (nums.size() vec(A, A + n); //vec传值不能达到A; 阅读全文
posted @ 2018-04-04 15:55 ranjiewen 阅读(158) 评论(0) 推荐(0) 编辑

上一页 1 2 3 4 5 6 ··· 9 下一页