- 博客(139)
- 收藏
- 关注
原创 【Leetcode】Word Ladder II (Backtracking)
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be changed at a timeEach intermediate word must exi
2014-11-14 01:34:00 555
原创 【Leetcode】Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""3
2014-11-13 10:53:19 417
原创 【Leetcode】Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3
2014-11-13 04:49:11 399
原创 【Leetcode】Word Break (DP)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"
2014-11-13 03:53:23 330
原创 【Leetcode】Decode Ways (DP)
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 numb
2014-11-12 12:22:43 537
转载 【Leetcode】Binary Tree Inorder Traversal (Tree Traversal)(转自Code_Ganker)
通常,实现二叉树的遍历有两个常用的方法:一是用递归,二是使用栈实现的迭代方法。下面分别介绍。递归应该最常用的算法,相信大家都了解,算法的时间复杂度是O(n), 而空间复杂度则是递归栈的大小,即O(logn)。代码如下: [java] view plaincopypublic ArrayList inorderTraversal(Tre
2014-11-11 23:27:49 454
转载 【Leetcode】Binary Tree Postorder Traversal (Tree Traversal)(转自Code_Ganker)
跟Binary Tree Inorder Traversal以及Binary Tree Preorder Traversal一样,二叉树的后序遍历我们还是介绍三种方法,第一种是递归,第二种是迭代方法,第三种是用线索二叉树。 递归还是那么简单,算法的时间复杂度是O(n), 而空间复杂度则是递归栈的大小,即O(logn)。代码如下:[java] view plaincopy
2014-11-11 23:21:30 436
转载 【Leetcode】Binary Tree Preorder Traversal (Tree Traversal) (转自Code_Ganker)
跟Binary Tree Inorder Traversal一样,二叉树的先序遍历我们仍然介绍三种方法,第一种是递归,第二种是迭代方法,第三种是用线索二叉树。递归是最简单的方法,算法的时间复杂度是O(n), 而空间复杂度则是递归栈的大小,即O(logn)。代码如下:[java] view plaincopypublic ArrayList p
2014-11-11 23:18:48 422
原创 【Leetcode】ZigZag Conversion
public String convert(String s, int nRows) { if (s == null || s.length() == 0 || nRows <= 0) return ""; if (nRows == 1) return s; StringBuilder res = new StringBuilder(); int size = 2 *
2014-11-11 08:44:26 355
原创 【Leetcode】Triangle (DP)
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-11-11 07:19:51 381
原创 【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a
2014-11-11 00:24:59 326
原创 【Leetcode】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.这道题典型cai'yon
2014-11-10 23:51:35 380
原创 【Leetcode】Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get
2014-11-10 23:10:45 355
原创 【Leetcode】Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".这是一道二进制加法题,注意进位和
2014-11-10 10:36:27 302
原创 【Leetcode】Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as
2014-11-10 10:06:50 1029
原创 【Leetcode】Jump Game II (DP)
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
2014-11-10 09:26:48 326
原创 【Leetcode】Jump Game (DP)
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.Determine i
2014-11-10 05:27:31 368
原创 【Leetcode】Next Permutation
public void nextPermutation(int[] num) { if(num==null||num.length==0) return; int i=num.length-2; while(i>=0&&num[i]>=num[i+1]) i--; if(i>=0) {
2014-11-10 04:29:46 288
原创 【Leetcode】Valid Sudoku
public boolean isValidSudoku(char[][] board) { if (board == null || board.length != 9 || board[0].length != 9) return false; for (int i = 0; i < 9; i++) { boolean[] table = new boolean[9];
2014-11-10 03:39:39 278
原创 【Leetcode】Subsets II (Backtracking)
public ArrayList> subsetsWithDup(int[] num) { ArrayList> result = new ArrayList>(); result.add(new ArrayList()); if (num == null || num.length == 0) return result; Arrays.sort(num); int s
2014-11-10 02:52:41 366
原创 【Leetcode】Subsets (Backtracking)
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-11-10 00:46:57 413
原创 【Leetcode】Gray Code (Pascal)
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-11-10 00:03:37 389
原创 【Leetcode】Minimum Depth of Binary Tree (Tree Judge)
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.递归思路,左右叶子记录各自的深度,返回左右叶子的最小深度+1 即
2014-11-09 12:02:31 354
原创 【Leetcode】Maximum Depth of Binary Tree (Tree Judge)
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-11-09 12:01:46 315
原创 【Leetcode】Validate Bunary Search Tree (Tree Judge)
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-11-09 11:57:43 380
原创 【Leetcode】Balanced Binary Tree (Tree Judge)
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-11-09 11:54:33 355
原创 【Leetcode】Symmetric Tree (Tree Judge)
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-11-09 11:53:35 318
原创 【Leetcode】Same Tree (Tree Judge)
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.
2014-11-09 11:51:26 345
原创 【Leetcode】Populating Next Right Point in Each Node II (TL)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.
2014-11-09 02:55:28 357
原创 【Leetcode】Populating Next Right Point in Each Node (TL)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.
2014-11-09 02:49:27 435
原创 【Leetcode】Flatten Binary Tree to Linked List (other)
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1
2014-11-09 00:17:36 459
原创 【Leetcode】Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.这道题huang'yan
2014-11-08 23:06:25 238
原创 【Leetcode】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.这道题和
2014-11-08 09:33:03 290
原创 【Leetcode】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.其实这道题弄清楚分段,思想还是挺简单的preorder的分段:
2014-11-08 05:22:01 360
原创 【Leetcode】Binary Tree Zigzag Level Order Traversal (Tree 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-11-08 04:23:36 252
原创 【Leetcode】Binary Tree Level Order Traversal II (Tree Traversal)
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-11-08 03:32:46 341
原创 【Leetcode】Binary Tree Level Order Traversal (Tree 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-11-08 03:07:52 291
原创 【Leetcode】Unique Binary Search Tree II (DP)
public ArrayList generateTrees(int n) { return helper(1, n); } private ArrayList helper(int left, int right) { ArrayList result = new ArrayList(); if (left > right) { result.add(null);
2014-11-08 01:17:36 301
原创 【Leetcode】Convert Sorted List to Binary Search Tree (BST)
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
2014-11-08 00:04:42 462
原创 【Leetcode】Convert Sorted Array to Binary Search Tree (BST)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.基本方法就是divide and conquerba
2014-11-07 23:13:01 281
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人