• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

随笔分类 -  Leetcode

上一页 1 ··· 23 24 25 26 27 28 29 30 31 32 下一页
Leetcode: Populating Next Right Pointers in Each Node II

摘要:层次递进法 复杂度 时间 O(N) 空间 O(1) 阅读全文
posted @ 2014-09-18 07:01 neverlandly 阅读(397) 评论(0) 推荐(0)
Leetcode: Populating Next Right Pointers in Each Node

摘要:复杂度 时间 O(N) 空间 O(N) 思路 相当于是Level Order遍历二叉树。通过一个Queue来控制每层的遍历,注意处理该层最后一个节点的特殊情况。此方法同样可解第二题。 没想到的方法:因为guarantee了是perfect binary tree 层次递进法 复杂度 时间 O(N) 阅读全文
posted @ 2014-09-18 06:38 neverlandly 阅读(365) 评论(0) 推荐(0)
Leetcode: Remove Duplicates from Sorted List II

摘要:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2-... 阅读全文
posted @ 2014-09-18 04:35 neverlandly 阅读(363) 评论(0) 推荐(0)
Leetcode: Copy List with Random Pointer

摘要:A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy ... 阅读全文
posted @ 2014-09-18 03:18 neverlandly 阅读(447) 评论(0) 推荐(0)
Leetcode: Sort List

摘要:记得Insert Sort List, 那个复杂度是O(N^2)的,这里要求O(nlogn),所以想到merge sort, 需要用到Merge Two Sorted List的方法(我写的merge函数) 第二遍代码:所以mergesort不好写在于它是一个递归里嵌套另一个递归,第一个递归不停地用 阅读全文
posted @ 2014-09-18 00:31 neverlandly 阅读(366) 评论(0) 推荐(0)
Leetcode: Insertion Sort List

摘要:Sort a linked list using insertion sort.我原本的想法是用额外的空间拷贝每一个节点,建立了一个新的sorted的LinkedList, 后来看到别人的做法不用建立新的LinkedList, 直接以原有List上的节点组成新的sorted的LinkedList。我... 阅读全文
posted @ 2014-09-17 12:12 neverlandly 阅读(384) 评论(0) 推荐(0)
Leetcode Binary Tree Zigzag level Order Traversal

摘要:参考这道题其实还是树的层序遍历Binary Tree Level Order Traversal。不过这里稍微做了一点变体。在Binary Tree Level Order Traversal中我们是维护了一个队列来完成遍历,而在这里为了使每次都倒序出来,我们很容易想到用栈的结构来完成这个操作。有一 阅读全文
posted @ 2014-09-16 14:17 neverlandly 阅读(383) 评论(0) 推荐(0)
Leetcode: Construct Binary Tree from Inorder and Postorder Traversal

摘要:Given inorder and postorder traversal of a tree, construct the binary tree.与Construct Binary Tree from Inorder and Preorder Traversal问题非常类似,唯一区别在于这一次确... 阅读全文
posted @ 2014-09-16 10:59 neverlandly 阅读(353) 评论(0) 推荐(0)
Leetcode: Construct Binary Tree from Preorder and Inorder Traversal

摘要:难度:95,参考了网上的思路。这道题是树中比较有难度的题目,需要根据先序遍历和中序遍历来构造出树来。这道题看似毫无头绪,其实梳理一下还是有章可循的。下面我们就用一个例子来解释如何构造出树。假设树的先序遍历是12453687,中序遍历是42516837。这里最重要的一点就是先序遍历可以提供根的所在,而 阅读全文
posted @ 2014-09-16 10:33 neverlandly 阅读(489) 评论(0) 推荐(0)
Leetcode: Flatten Binary Tree to Linked List

摘要:第二遍做法:不用ArrayList来wrap up previous node, 直接把previous node做成全局变量 第一遍做法:用递归来解决,维护先序遍历的前一个结点pre,然后每次把pre的左结点置空,右结点设为当前结点。这里需要注意的一个问题就是我们要先把左右子结点(Code Gan 阅读全文
posted @ 2014-09-16 04:24 neverlandly 阅读(482) 评论(0) 推荐(0)
Leetcode: Binary Tree Level Order Transversal II

摘要:第二遍方法: 这道题在groupon面经里面有,有一个follow up 是能不能右对齐输出。那就在29行记录每一行的最大size,然后在输出的时候根据最大size补齐空格 在Binary Tree Level Order Transversal的基础上难度:20,只需要对最后结果做一个倒序就好。格 阅读全文
posted @ 2014-09-15 13:30 neverlandly 阅读(383) 评论(0) 推荐(0)
Leetcode: Binary Tree Level Order Traversal

摘要:Best approach: 把树看成一个有向图,进行BFS。BST的通常做法都是维护一个队列。这道题的难度在于,如何在队列不断的入队出队操作中,确定哪些node是一个层次的。想了很久没什么好办法,参考了网上的做法,发现他在维护两个数:一个是父节点所在层次的节点在当前队列中的数目ParentNumI 阅读全文
posted @ 2014-09-15 12:52 neverlandly 阅读(568) 评论(0) 推荐(0)
Leetcode: Binary Tree Postorder Transversal

摘要:难度:70 recursive方法很直接: Iterative 最优做法: pre-order traversal is root-left-right. post-order traversal is left-right-root. We can modify pre-order travers 阅读全文
posted @ 2014-09-15 10:20 neverlandly 阅读(281) 评论(0) 推荐(0)
Leetcode: Binary Tree Inorder Transversal

摘要:recursive 方法: Iterative method: 参考了一下网上的思路,其实就是用一个栈来模拟递归的过程。所以算法时间复杂度也是O(n),空间复杂度是栈的大小O(logn)。 阅读全文
posted @ 2014-09-15 08:49 neverlandly 阅读(415) 评论(0) 推荐(0)
Leetcode: Word Break II

摘要:难度:98,参考了别人的思路:这道题目要求跟Word Break比较类似,不过返回的结果不仅要知道能不能break,如果可以还要返回所有合法结果。一般来说这种要求会让动态规划的效果减弱很多,因为我们要在过程中记录下所有的合法结果,中间的操作会使得算法的复杂度不再是动态规划的两层循环,因为每次迭代中还 阅读全文
posted @ 2014-09-14 11:21 neverlandly 阅读(933) 评论(0) 推荐(0)
Leetcode: Word Break

摘要:首先我们要决定要存储什么历史信息以及用什么数据结构来存储信息。然后是最重要的递推式,就是如从存储的历史信息中得到当前步的结果。最后我们需要考虑的就是起始条件的值。 接下来我们套用上面的思路来解这道题。首先我们要存储的历史信息res[i]是表示到字符串s的第i个元素为止能不能用字典中的词来表示,我们需 阅读全文
posted @ 2014-09-13 12:22 neverlandly 阅读(415) 评论(0) 推荐(0)
Leetcode: Maximum Subarray

摘要:思想难度:99,操作难度:50。思想大于行动的一项有力证明。这是一道非常经典的动态规划的题目,用到的思路我们在别的动态规划题目中也很常用,以后我们称为”局部最优和全局最优解法“。差不多的DP问题参见:Jump Game, Jump Game II基本思路是这样的,在每一步,我们维护两个变量,一个是全 阅读全文
posted @ 2014-09-12 13:40 neverlandly 阅读(344) 评论(0) 推荐(0)
Leetcode: Jump Game II

摘要:难度:89,这道题参考了网上的解法:和Jump Game很像,只是原来的全局最优现在要分成step步最优和step-1步最优(假设当前步数是step)。当走到超过step-1步最远的位置时,说明step-1不能到达当前一步,我们就可以更新步数,将step+1。时间复杂度仍然是O(n),空间复杂度也是 阅读全文
posted @ 2014-09-12 12:49 neverlandly 阅读(316) 评论(0) 推荐(0)
Leetcode: Jump Game

摘要:Greedy Solution 阅读全文
posted @ 2014-09-12 05:28 neverlandly 阅读(401) 评论(0) 推荐(0)
Leetcode: N-Queens II

摘要:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.跟N-Queen的考虑方式完全一样,NP问题,用循环递... 阅读全文
posted @ 2014-09-11 12:49 neverlandly 阅读(355) 评论(0) 推荐(0)

上一页 1 ··· 23 24 25 26 27 28 29 30 31 32 下一页
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3