
Medium
文章平均质量分 71
ljffdream
这个作者很懒,什么都没留下…
展开
-
【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.【思路】pretty similar with that转载 2015-09-03 03:10:58 · 411 阅读 · 0 评论 -
【Leetcode】Rotate List
【题目】Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.【思路】实质就是n-k个元素挪到了链子的前面, 第原创 2015-07-29 23:06:14 · 261 阅读 · 0 评论 -
【Leetcode】Kth Largest Element in an Array
【题目】Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, retu转载 2015-07-29 10:07:43 · 567 阅读 · 0 评论 -
【Leetcode】Valid 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 nod转载 2015-07-17 23:26:37 · 305 阅读 · 0 评论 -
【Leetcode】Combination sum 1,2
【题目】【分析】首先,先sort,如果target > 0:【代码】using recursive 这恐怕的 O(n2)...过程:before recursion : [2]before recursion : [2, 2]before recursion : [2, 2, 2]after recursion and remove :转载 2015-08-24 16:47:10 · 355 阅读 · 0 评论 -
【Leetcode】Search for a range
【题目】【思路】The problem can be simply broken down as two binary searches for the begining and end of the range, respectively:First let's find the left boundary of the range. We ini转载 2015-08-23 21:09:39 · 292 阅读 · 0 评论 -
【Leetcode】Lowest common treenode in binary tree
【题目】Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between tw转载 2015-07-28 22:41:59 · 491 阅读 · 0 评论 -
【Leetcode】Sort List in O(nlogn) O(1)space
【题目】【思路】First of all, allow me to explain the meaning of strict O(1) auxiliary space complexity.It means the maximum number of memory used by the program, except the memory taken by th转载 2015-07-27 20:42:43 · 321 阅读 · 0 评论 -
【Leetcode】Path sum 2
【题目】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转载 2015-07-27 22:44:56 · 325 阅读 · 0 评论 -
[Leetcode]Word Ladder
[题目]Given two words (beginWord and endWord), and a dictionary, find the length ofshortest transformation sequence frombeginWord to endWord, such that:Only one letter can be changed at a ti转载 2015-07-06 09:12:15 · 480 阅读 · 0 评论 -
【Leetcode】Rotate Image
【题目】You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).【思路】The idea was firstly transpose the matrix and then flip it symmetrically.转载 2015-08-26 14:31:59 · 269 阅读 · 0 评论 -
【Leetcode】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转载 2015-08-03 20:21:03 · 280 阅读 · 0 评论 -
[Leetcode]Word Search
[timu]Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontal转载 2015-09-02 07:51:04 · 288 阅读 · 0 评论 -
【Leetcode】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], [转载 2015-09-04 11:08:48 · 366 阅读 · 0 评论 -
【Leetcode】subsets2T
【题目】Given a collection of integers thatmight contain duplicates, nums,return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contai转载 2015-09-03 00:39:53 · 323 阅读 · 0 评论 -
【Leetcode】Longest Palindrome
【题目】Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.【思路】中心检测转载 2015-09-09 10:25:31 · 422 阅读 · 0 评论 -
【Leetcode】Set matrix zeros
【题目】【思路】My idea is simple: store states of each row in the first of that row, and store states of each column in the first of that column. Because the state of row0 and the state of column0转载 2015-09-01 23:10:47 · 354 阅读 · 0 评论 -
[Leetcode]Search a 2D matrix
[timu]Search a 2D MatrixTotal Accepted: 52191 Total Submissions: 164580Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the followin原创 2015-09-02 03:29:54 · 290 阅读 · 0 评论 -
【Leetcode】Minimum sum path
【题目】Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down转载 2015-09-01 11:30:47 · 234 阅读 · 0 评论 -
【Leetcode】Unique paths 2
【题目】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转载 2015-09-01 10:04:16 · 221 阅读 · 0 评论 -
【Leetcode】Spiral Matrix
【题目】Given aninteger n,generate a square matrix filled with elements from 1 to n2 inspiral order.For example,Given n = 3,Youshould return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ]转载 2015-09-01 00:52:31 · 259 阅读 · 0 评论 -
[Leetcode]sort Color
[timu] Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adja转载 2015-09-02 03:55:21 · 271 阅读 · 0 评论 -
【Leetcode】Subsets
【题目】Given a set of distinct integers, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.转载 2015-07-12 21:17:00 · 361 阅读 · 0 评论 -
【Leetcode】Find the kth in the BST
[题目][思路]BST的特点就是排序性,左边的节点一定比root小,右边的节点一定比root大。另外就是二分性。找第k个,可以先看左边树节点的个数,小于k的话,就保证了肯定是在右边。那么去找右边的k-left.k - left,就变成了新的k.数root所在的树的节点数。这个就是用递归实现。想法就是,如果root自己本身就是null,那么就return 0; 否则至转载 2015-07-08 21:28:09 · 525 阅读 · 0 评论 -
【Leetcode】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转载 2015-06-14 13:02:44 · 290 阅读 · 0 评论 -
【Leetcode】Generate Parent
【题目】Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()((转载 2015-01-12 14:22:55 · 345 阅读 · 0 评论 -
【Leetcode】Sort Colors
【题目】Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the原创 2014-12-07 10:20:07 · 355 阅读 · 0 评论 -
【Leetcode】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,转载 2014-12-05 17:30:17 · 600 阅读 · 0 评论 -
【LeetCode】Single Number
【题目】Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without u原创 2014-12-04 10:22:37 · 313 阅读 · 0 评论 -
【Leetcode】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转载 2015-01-06 17:05:05 · 343 阅读 · 0 评论 -
【LeetCode】Convert Sorted Array to Binary Search Tree
leetcode原创 2014-11-26 11:05:06 · 300 阅读 · 0 评论 -
【Leetcode】Reverse word in a string
【题目】Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What con原创 2014-11-27 23:28:33 · 572 阅读 · 0 评论 -
【Leetcode】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.【思路】这道题目就和array不一样,因为是linkedlist所以,不能随意访问到中间的元素,那么就只能一步一步从头开始顺序访问,从列表到BST,我们能感受到构原创 2014-11-26 12:59:59 · 314 阅读 · 0 评论 -
【Leetcode】Grey 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 se转载 2014-12-24 23:36:22 · 363 阅读 · 0 评论 -
【Leetcode】integer to Roman
【题目】Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.【分析】只要是遵守那个转换规则就好吧。然后就是数学处理。【代码1】private static LinkedHashMap numToRoman原创 2014-12-24 18:58:12 · 279 阅读 · 0 评论 -
【Leetcode】Swap Nodes in Pairs
【题目】Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant原创 2015-01-03 11:53:30 · 399 阅读 · 0 评论 -
【LeetCode】Find Minimum In Rotated Sorted Array 1 and 2
【题目】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转载 2015-01-05 10:20:39 · 305 阅读 · 0 评论 -
[Leetcode]Sorted in rotated Array 2
[题目] medianFollow 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 tar转载 2015-06-12 09:13:22 · 311 阅读 · 0 评论 -
[Leetcode]House Robber2
[题目]Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. T转载 2015-07-08 09:15:07 · 527 阅读 · 0 评论 -
[Leetcode]Number of Island
[题目]Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You ma转载 2015-07-07 09:55:24 · 289 阅读 · 0 评论