自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(128)
  • 收藏
  • 关注

原创 Largest BST in a Binary Tree

一到面试题,挺有意思的。 简单做法是对于每个node都进行一遍isBST的测试,找出最大的那个。 public static int largestBSTNaive(TreeNode root){ if(isBST(root)){ return size(root); } return Math.max(largestBSTNaive(root.left), largestB

2015-03-05 12:11:59 1874

原创 LeetCode刷题笔录dungeon game

The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially p

2015-01-08 04:27:02 6027

原创 LeetCode刷提笔录Scramble String

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / \ gr

2014-12-18 07:05:53 849

原创 LeetCode刷题笔录Interleaving String

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return

2014-12-16 06:46:06 678

原创 LeetCode刷题笔录Sort List

Sort a linked list in O(n log n) time using constant space complexity. 挺有意思的题,这里用了merge sort,可以利用到merge sorted list 那题的代码 /** * Definition for singly-linked list. * class ListNode { *

2014-11-29 13:18:48 628

原创 LeetCode刷题笔录Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 这题要用到

2014-11-14 13:29:45 745

原创 LeetCode刷题笔录Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "

2014-11-07 16:18:23 841

原创 LeetCode刷题笔录Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()",

2014-11-07 15:15:14 730

原创 LeetCode刷题笔录Wildcard Matching

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover t

2014-11-07 08:01:15 631

原创 LeetCode刷题笔录Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 两个点是必然gong'xian

2014-11-07 04:45:29 734

原创 LeetCode刷题笔录Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width o

2014-11-06 14:44:49 792

原创 LeetCode刷题笔录Binary Tree Maximum Path Sum

这题试了很多次也没成功,从网上找了段d

2014-11-05 12:47:41 678

原创 LeetCode刷题笔录Simplify Path

Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did

2014-10-30 08:19:00 638

原创 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-10-29 08:37:23 839

原创 LeetCode刷题笔录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-10-28 09:30:25 601

原创 LeetCode刷题笔录LRUCache

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

2014-10-26 01:30:49 609

原创 LeetCode刷题笔录Reverse Words in a String

https://oj.leetcode.com/problems/reverse-words-in-a-string/ 我的做法是从后往前扫描,遇到空格就

2014-10-16 12:34:14 774

原创 LeetCode刷题笔录Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1

2014-10-15 06:02:49 574

原创 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->3->3->4->4->5, return 1->2->5. Given 1->

2014-10-08 05:38:14 713

原创 LeetCode刷题笔录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 satisfy t

2014-10-06 07:02:39 526

原创 LeetCode刷题笔录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-10-05 08:51:01 618

原创 LeetCode刷题笔录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-10-05 04:19:06 552

原创 LeetCode刷题笔录Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","

2014-10-04 00:45:14 594

原创 LeetCode刷题笔录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

2014-10-03 04:22:47 555

原创 LeetCode刷题笔录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-10-03 03:58:30 659

原创 LeetCode刷题笔录Spiral Matrix II

Given 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 matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [

2014-10-02 08:41:30 534

原创 LeetCode刷题笔录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-10-02 06:52:48 482

原创 LeetCode刷题笔录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-10-01 10:31:14 533

原创 LeetCode刷题笔录Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be non

2014-09-03 05:03:55 530

原创 LeetCode刷题笔录Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 具体一位一位地加就行了,考虑进位的问题。还有最后记得把生成的string反过来再返回,因为我们是从最低位开始加的。 public class Solu

2014-08-30 00:51:31 1076

原创 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-08-29 11:57:32 601

原创 Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of convertin

2014-08-26 04:40:54 541

原创 LeetCode刷题笔录Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 和Integer to Roman那题一样挺无聊的。

2014-08-22 05:46:30 600

原创 Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 没啥算法技术含量,gaomi

2014-08-22 05:26:18 448

原创 LeetCode刷题笔录Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].

2014-08-21 08:55:55 608

原创 LeetCode刷题笔录Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 用一个priority queue,时间复杂度O(nklogn)

2014-08-21 05:53:45 646

原创 LeetCode刷题笔录N-Queens II

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions.

2014-08-21 04:09:31 557

原创 LeetCode刷题笔录Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the

2014-08-20 23:55:03 566

原创 LeetCode刷题笔录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-08-20 00:08:37 691

原创 LeetCode刷题笔录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.

2014-08-19 10:58:50 722

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除