LeetCode
文章平均质量分 66
wjz748305545
这个作者很懒,什么都没留下…
展开
-
Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2014-02-19 22:50:52 · 578 阅读 · 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-02-25 22:36:26 · 468 阅读 · 0 评论 -
Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?题意:给定一个链表,判断是否有环由于这题出现的频率太高,我也不小心很早就看过它的做法,SO,直接贴代码吧!PS:对于链表关键要考虑它的健壮性,异常情原创 2014-02-25 19:59:51 · 449 阅读 · 0 评论 -
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?题意:在Linked List Cycle 基础上增加了难度,要求求出有环时的起始环原创 2014-02-25 22:12:21 · 500 阅读 · 0 评论 -
Insertion Sort List ——链表插入排序
注意将链表拆分,和节点的调整ListNode *insertionSortList(ListNode *head) { ListNode *p = head; ListNode *q = head; ListNode *pre = NULL; if(!p || !p->next) return head; q = q->next; head->next = NULL; whi原创 2014-03-05 15:06:16 · 632 阅读 · 0 评论 -
Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2014-03-10 22:56:15 · 440 阅读 · 0 评论 -
Evaluate Reverse Polish Notation
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",原创 2014-03-04 20:45:12 · 517 阅读 · 0 评论 -
Sort List——链表的快速/归并排序
用O(nlgn)时间对单链表排序,常量空间参考:http://blog.csdn.net/xudli/article/details/16819545思路:排序——O(nlgn)——》快排或者归并,选择归并,将链表分成两等分进行归并排序ListNode *mergeSort(ListNode *head){// ListNode *p=head; ListNode *q=head;原创 2014-03-04 23:07:14 · 742 阅读 · 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原创 2014-03-05 16:50:53 · 430 阅读 · 0 评论 -
Same Tree
题意:判断两颗二叉树是否相同递归实现:public boolean isSameTree(TreeNode p, TreeNode q) { if(p==null && q==null) return true; if(q != null && p != null){ return (p.val == q.val)原创 2014-03-10 21:47:46 · 362 阅读 · 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-03-10 22:17:59 · 396 阅读 · 0 评论 -
Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota原创 2014-03-10 23:09:51 · 375 阅读 · 0 评论 -
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 of the list.题意:深度复制一个有随机指针的链表由于在《剑指Off原创 2014-03-05 21:30:50 · 431 阅读 · 0 评论 -
Balanced Binary Tree
题意:判断是否为二叉树解法一:递归,由于重复计算次数多,所以时间复杂度不能忍,暂且贴出来吧/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val原创 2014-03-11 20:53:27 · 423 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.题意:根据二叉树的前序遍历和中序遍历求出二叉树,假定没有重复值/** * Definition for bi原创 2014-03-12 20:23:41 · 419 阅读 · 0 评论 -
【修正二叉树】Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devis原创 2014-03-12 22:48:49 · 556 阅读 · 0 评论 -
【二叉树是否对称】Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f原创 2014-03-12 21:03:37 · 661 阅读 · 0 评论 -
【判断二叉搜索树】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.Th原创 2014-03-12 23:06:59 · 463 阅读 · 0 评论 -
Minimum Depth of Binary Tree
题意:求二叉树的最小路径先理解什么是最小路径,即从根节点到叶节点的距离最短,所以要判断节点是否为叶节点/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x)原创 2014-03-11 21:26:33 · 371 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.题意:将有序链表转成一颗平衡二叉树,所以要找到链表的中间节点进行递归转换/** * Definition for singly-linked list. * publ原创 2014-03-11 22:31:42 · 373 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题意:将有序数组转为平衡二叉树,找到中间节点,递归转换成平衡二叉树/** * Definition for binary tree * public class TreeNode { *原创 2014-03-11 22:39:54 · 354 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.题意:由中序遍历和后续遍历确定二叉树,假定没有重复值/** * Defin原创 2014-03-12 20:12:58 · 469 阅读 · 0 评论 -
【DFS】Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Order原创 2014-03-13 22:15:29 · 467 阅读 · 0 评论 -
Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3原创 2014-03-13 20:09:32 · 401 阅读 · 0 评论 -
【二叉树先序遍历】Binary Tree Preorder Traversal
题意:二叉树的前序遍历这里先提供递归实现,后续补上public ArrayList preorderTraversal(TreeNode root) { if(root == null) return new ArrayList(); ArrayList list = new ArrayList(); ArrayList l原创 2014-03-08 18:07:41 · 631 阅读 · 0 评论 -
【二叉树的后续遍历】Binary Tree Postorder Traversal
题意:二叉树的后续遍历这里先使用递归的形式实现,以后再数据结构中补上非递归实现,对边界条件的处理要想好!public ArrayList postorderTraversal(TreeNode root) { if(root == null) return new ArrayList(); ArrayList list = new ArrayList();原创 2014-03-08 18:02:43 · 667 阅读 · 0 评论 -
Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to原创 2014-03-08 21:01:08 · 459 阅读 · 0 评论 -
Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \原创 2014-03-13 19:29:15 · 390 阅读 · 0 评论 -
Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary原创 2014-03-09 22:54:38 · 498 阅读 · 0 评论 -
【二叉树中序遍历】Binary Tree Inorder Traversal
一、递归实现:public ArrayList inorderTraversal(TreeNode root) { ArrayList list = new ArrayList(); if(root == null) return list; ArrayList leftList = inorderTraversal(root.原创 2014-03-09 20:42:50 · 723 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},原创 2014-03-09 22:24:26 · 489 阅读 · 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-03-09 21:22:09 · 992 阅读 · 0 评论 -
【DFS】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-03-18 20:58:09 · 519 阅读 · 0 评论 -
【DFS】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-03-18 21:48:16 · 445 阅读 · 0 评论 -
【DP】Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the原创 2014-03-31 10:14:30 · 401 阅读 · 0 评论 -
【DFS】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-03-18 20:56:57 · 385 阅读 · 0 评论 -
【最长公共前缀】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.public class Solution { public String longestCommonPrefix(String[] strs) { String prefix = "";原创 2014-04-16 19:33:27 · 695 阅读 · 0 评论 -
【DP】Unique Paths II
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the原创 2014-03-31 10:40:11 · 381 阅读 · 0 评论 -
【DP】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Return原创 2014-03-31 22:34:50 · 375 阅读 · 0 评论 -
【DP】Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [原创 2014-03-31 17:33:58 · 402 阅读 · 0 评论