`
frank-liu
  • 浏览: 1665588 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
文章列表
问题描述   给定一个连通的图,确定一个删除节点的顺序,使得每次删除节点之后图的剩余部分依然是连通的。要求算法的时间复杂度是O(V + E)。   分析   这个问题粗看起来比较复杂。因为对于一个图来说,假设我们要删除一个节点,那么它所对应的边都要被删掉。光去遍历所有的节点找到所有包含某个要删除节点的边都要费很大的劲。所以不能单纯的用一个个查找然后删除的方法。   我们来看这个问题的要求,既然是希望删除了某个节点之后保证图还是连通的,我们在当前要删除的节点就可能是图中间某个独立的边上的节点,或者是一个环上面的节点。因为删除了这个节点之后,并不会破坏图的连通性。但是这个节点不能在图的 ...
简介   最近在学习一些图相关的算法时碰到一个比较有意思的问题。就是关于图中间桥的问题。在图中,一般的边是用于连接两个节点的。如果结合图的连通性来考虑,假设一个边连着图的两个部分,如果我们将这个边去掉,那么将使得图变成两个分割开的部分。那么,这个时候我们称这个边是桥。   那么,对于一个图来说,我们能否找到一些这样的桥呢?   分析   在找到具体的规律之前,我们先看一个图的示例:   从上图来看,我们需要找到一个边,这个边如果去掉之后会使得整个图被分割成两个不相连的部分。很明显,节点1和4构成的边恰好符合这个条件。而其他的边则不符合这个条件。那么这些不符合条件的边像< ...
问题描述 Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root. For example:Given a ...
问题描述: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 ...
问题描述: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 原问题链接:https://leetcode.com/problems/find-minimum-in-rotated-sor ...
问题描述: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 原问题链接:https://leetcode.com/problems/maximum-product-subarray/   问题分析   这个问题在之前的文 ...
问题描述: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. click to show clarification. Clarification:   What constitutes a ...
问题描述: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples:           ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) - ...
问题描述: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 原问题链接:https://leetcode.com/problems/max-points-on-a-line/   问题分析   给定一个平面上若干个点来说,要计算有多少个点在一条线上,我们需要选择每个节点作为起点,看它到所有其他的点能连成一条线的有多少个。对于每个点的情况,取它所能覆盖的最大点个数。   在具体的实现中,有若干种情况需要考虑。对于一个点来说,可能有其 ...
问题描述: Sort a linked list using insertion sort. 原问题链接:https://leetcode.com/problems/sort-list/   问题分析   有时候,往往是一些看似简单的问题描述 ,其解决方法越麻烦。这里问题描述确实简单,对于一个链表来说,我们需 ...
问题描述: Sort a linked list using insertion sort. 原问题链接:https://leetcode.com/problems/insertion-sort-list/   问题分析   这里因为是针对链表进行插入排序,其过程和普通数组的插入排序有点不一样。相对来说因为没有直接的索引访问,它要复杂不少。在针对链表的插入排序实现前,我们先看看基于数组的插入排序过程。对于数组的插入排序,它是从索引为1的元素开始,每次和它前面的元素比较,碰到一个比当前大的元素,就和这个元素换个位置,一直到它之前的元素比它小。   在上面的过程,这是针对可以很方便的 ...
问题描述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value) - Set or insert the value if t ...
问题描述: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3   return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 原问题链接:https://leetcode.com/problems/binary-tree-post ...
问题描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3   return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 原问题链接:https://leetcode.com ...
问题描述: Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 原问题链接:https://leetcode.com/problems/reorder-list/   问题分析   在写具体的代码实现之前,我们先看问题的要求。它要求是 ...
Global site tag (gtag.js) - Google Analytics