LeetCode
文章平均质量分 63
shiquxinkong
这个作者很懒,什么都没留下…
展开
-
Reverse Linked List II
问题:Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisf原创 2013-12-28 20:10:58 · 507 阅读 · 0 评论 -
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->3->3->4->4->5, return 1->2->5.Given 1->1-原创 2013-12-29 14:55:12 · 598 阅读 · 0 评论 -
Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal i原创 2013-12-29 01:07:09 · 1054 阅读 · 0 评论 -
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.class Solution {public原创 2013-12-29 02:29:07 · 491 阅读 · 0 评论 -
Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2013-12-29 16:37:44 · 563 阅读 · 0 评论 -
Triangle
问题分析:典型的DP问题,这里我们用A[ i, j ]表示到 [ i,j ] 位置的最小和,根据题目的规则,只能向下方或右下方移动,那么A[ i,j ] = min{ A[ i + 1, j ], A[ i + 1, j + 1 ] } + a[ i, j ] 。这样我们就需要一个跟给定数组规模一样的额外数组,题目中建议O(n)的额外空间,我们看下能不能实现,观察状态转移方程我们不难发现,当前的值原创 2013-12-28 18:25:40 · 570 阅读 · 0 评论 -
Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value./** * Defi原创 2013-12-30 23:56:20 · 539 阅读 · 0 评论 -
Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists第一次觉得没什么可说的题,连废话都没的说。//codeclass Solution原创 2013-12-31 11:06:43 · 520 阅读 · 0 评论 -
Symmetric Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.解决了same tree原创 2013-12-31 00:16:06 · 535 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node问题另一种解法
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. 见<<圣经>>里说的话还是有道理的,里面讲到“已有的事后必再有,已行的事后必再行,日光之下并无新事”,一切皆属空虚。原创 2014-01-01 13:44:15 · 1120 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant原创 2014-01-01 15:10:45 · 985 阅读 · 0 评论 -
Flatten Binary Tree to Linked List
问题明显递归操作比较简单,我们看下草图:紫色矩形表示进行了相同处理后的结果,在链接root和左子树的时候是可以直接操作的,但是我们这么去链接左子树和右子树呢?应该是左子树中最后的那个结点跟右子树的root链接,所以我们需要去找到左子树的最后(最右)那个非空结点。//code/** * Definition for binary tree * struct TreeNode原创 2014-01-01 16:56:26 · 836 阅读 · 0 评论 -
Sum Root to Leaf Numbers
这个问题用递归是可定的,最终的结果的左子树的结果加上右子树的结果,这只完成程序的一部分,问题是具体怎么计算呢?想想我们将字符串转换为整数的操作,例如给定数串“98765\0”,1)0 * 10 + 9 = 9;2)9 * 10 + 8 = 98; 3)98 * 10 + 7 = 987;4)987 * 10 + 6 = 9876;5)9876 * 10 + 5 = 98765。怎么样有感觉了吗?我原创 2013-12-31 22:11:16 · 534 阅读 · 0 评论 -
Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.class Solution {public: int m原创 2014-01-01 19:16:57 · 670 阅读 · 0 评论 -
Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.相对于求最小值而言,求最大值就更简单了。直接递归就可原创 2014-01-01 19:22:45 · 583 阅读 · 0 评论 -
Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum原创 2014-01-01 19:37:53 · 717 阅读 · 0 评论 -
Evaluate Reverse Polish Notation
逆波兰式就是二叉树树后根遍历的结果,逆波兰式在借助栈进行算式运算的时候一个很好的优势就是不用比较栈顶操作符的优先级,操作过程很简单:1)若是操作数,入栈2)若是操作符,就像扁担挑水一样,在栈里挑出两个数进行运算(注意第一操作数和第二操作数),将结果作为操作数再次压入栈中,重复操作,直到表达式末尾。//["4", "13", "5", "/", "+"] -> (4 + (13 / 5)原创 2014-01-02 14:33:01 · 712 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
这个问题的原始版本见此基础上的问题,只要把原来问题的结果逆置下就可以了,但感觉这样做有点挫,就想别的办法,觉的只要把插值的操作放在左右子树的递归之后来处理这不就对了,果断敲入,果断出错了,为什么?因为加入右子树比左子树深度要深,那么左子树就会在整棵树的深度为获得之前进行插入,没得到整体的深度,可想而知插入的位置是错误的。见例子: void levelNode(TreeNode *root, i原创 2014-01-02 17:00:36 · 782 阅读 · 0 评论 -
Binary Tree Maximum Path Sum
如果这个例子你得到的结果是 15,那还是看看上面的链接和下面的分析,如果得到的是 11,那请跳过,直接看代码吧。在求最大连续子段和问题时,我们总是考虑对当前元素的处理,是加入还是不加入到序列里,若加入到序列里,则继续同样处理下一个元素,若不加入,则先前得到的部分和与我无关,我重新做人,丢掉一切从0开始。那么对于树结构而言,首先我们想到的处理策略都是递归,那么既然递归我们就从底向上开始处理了(从root开始,只能说呵呵了),这跟DP也很像,抛开特殊情况,我们假设当前处理的节点是 2,那么我们怎么处理2 呢原创 2014-01-01 23:58:53 · 1241 阅读 · 0 评论 -
Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20原创 2014-01-02 16:01:50 · 759 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node
原题点这给定一个完全二叉树,不同的是每个节点有个额外的next域,目的是通过这些next域将同层的所有节点串接起来。问题结合着程序和给定的实例,自己走一遍就明白了,如果思路不是很清晰,建议自己要么把这个例子走一遍,要么自己画个更复杂的模拟程序走一遍,否则没效果。O(∩_∩)O~图画的有点像彩票开奖的节奏啊。。。//code/** * Definition for原创 2014-01-01 00:59:38 · 908 阅读 · 0 评论 -
Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each原创 2014-01-02 17:50:44 · 600 阅读 · 0 评论 -
Spiral Matrix II
http://blog.csdn.net/shiquxinkongGiven an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following ma原创 2014-01-03 14:05:40 · 660 阅读 · 0 评论 -
Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]原创 2014-01-03 17:05:43 · 770 阅读 · 0 评论 -
Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2014-01-03 21:05:26 · 730 阅读 · 0 评论 -
Gas Station
这个问题网上的最简单版本的确是简单的一塌糊涂,O(n)的复杂度,我们来理一理这种简单的思路。假设A的每个月的工资和花费是固定的,而且每年都是不变。如果到了第 i 个月发现自己的积蓄不足以满足花费的时候,当然这日子就没法过了。现在A要交个女朋友,但交女朋友有条件,必须在他们认识后的第一个九月份买个苹果5土豪金,我们帮他看下,以他的工资和花费情况,在几月份开始交女朋友,能够保证满足女友的要求,抱的原创 2014-01-05 20:27:35 · 657 阅读 · 0 评论 -
Binary Tree Postorder Traversal
二叉树的非递归后序遍历,如题目中所言,递归算法就比较简单了,非递归算法中,常见的教科书式的也比较中规中矩,不经意在csdn上看到了一篇二叉树遍历的文章,博主在非递归后续遍历的实现上想法很新颖。再次按其思想实现了下,很简洁。参考博主链接:http://blog.csdn.net/sgbfblog/article/details/7773103代码:class Solution {pub原创 2013-12-22 21:33:27 · 549 阅读 · 0 评论 -
Binary Tree Preorder Traversal
二叉树的非递归先根遍历,这里想说的是,尽管先根遍历的递归算法实现起来很简单,就几行,但是其思想还是应该细细体会的,在此不细表。先根遍历就是先访问根,访问(visit)是个抽象的泛称,具体怎么访问看题目要求,是简单的输出还是修改不一而定。然后接着访问左孩子,再访问右孩子。重复一遍,根->左->右。我们用栈来实现。栈的特性后进先出,所以进栈的时候我们要按遍历的逆序进,先右后左。代码:pub原创 2013-12-22 21:53:10 · 564 阅读 · 0 评论 -
Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2014-01-05 18:15:16 · 676 阅读 · 0 评论 -
Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of原创 2014-01-05 21:40:46 · 777 阅读 · 0 评论 -
Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu原创 2014-01-06 15:58:27 · 812 阅读 · 0 评论 -
Plus One
Given a number represented as an array of digits, plus one to the number.class Solution {public: vector plusOne(vector &digits) { vector re; int carry = 1; for(int i = d原创 2014-01-06 17:15:27 · 654 阅读 · 0 评论 -
Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".class Solution {public: string addBinary(string a, string b) { int la = a.len原创 2014-01-07 12:37:28 · 710 阅读 · 0 评论 -
Reorder List
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原创 2013-12-23 23:53:30 · 650 阅读 · 0 评论 -
Subsets
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For exa原创 2014-01-06 20:09:57 · 731 阅读 · 0 评论 -
Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain dupli原创 2014-01-06 21:39:41 · 792 阅读 · 0 评论 -
Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] ha原创 2014-01-07 00:00:50 · 978 阅读 · 0 评论 -
Single Number
原题链接问题:给定一个整形数组及数组大小,其中数组中只有一个元素出现了一次,其他均出现两次,找出这个元素。时间复杂度要求为线性,做好不用额外的存储空间。看了这个题,尤其是看了博主doc_sgl的解法后,才深深的感觉到,这tm玩的就是数学啊,果断把自己写的代码删了一百遍。经典啊。。各位看官想到了没,数学,不用其他空间,重复出现了两次,就一个一次,提示到这了,想到没,想不到没关系,这墙原创 2013-12-24 00:44:35 · 621 阅读 · 0 评论 -
Linked List Cycle II
原题链接问题:给定一个单向链表,若存在环,则返回环的其实结点,否则返回NULL。在解这个问题之前,先看个数学问题,或者是体育现象,我们都知道马拉松比赛,运动员需要在体育场外跑一段距离,然后进入体育场跑道。如下草图设运动员在场外需要跑的路段AB长为k,环形跑道的周长为c,跑道的入口为B,进入后沿跑道顺时针方向跑,现有选手甲乙,乙每秒跑1千米,甲更生猛,每秒跑2千米(有的看官可能会原创 2013-12-24 12:09:57 · 704 阅读 · 0 评论 -
Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2014-01-08 18:58:13 · 665 阅读 · 0 评论