leetcode
文章平均质量分 70
ljffdream
这个作者很懒,什么都没留下…
展开
-
【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 · 282 阅读 · 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 · 283 阅读 · 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 · 552 阅读 · 0 评论 -
【Leetcode】Panlidrome Linked List
【题目】Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?【思路】panlidrome的话,回文,就是前后是一样的。第一个和最后一个一样, 第二个和倒数第二个一样。linked list转载 2015-07-17 11:27:36 · 319 阅读 · 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 · 342 阅读 · 0 评论 -
【Leetcode】First Missing Positive
【题目】Given an unsortedinteger array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and us转载 2015-08-24 23:21:41 · 291 阅读 · 0 评论 -
【Leetcode】Merge K Sorted Linked List
【题目】Merge k sorted Linked Lists and return it as one sorted lists. Analyze and describe its complexity .【思路】leetcode clean book【代码】 private static final Comparator listComparator = new转载 2015-07-16 21:39:48 · 357 阅读 · 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 · 252 阅读 · 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 · 260 阅读 · 0 评论 -
【Leetcode】Rain trapping
【题目】 Given n non-negative integers representing anelevation map where the width of each bar is 1, compute how much water it isable to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1转载 2015-08-25 22:18:54 · 344 阅读 · 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 · 483 阅读 · 0 评论 -
[Leetcode]Power of Two
[题目]Given an integer, write a function to determine if it is a power of two.[思路]这道题的思路还是蛮简单的说,0的时候是false,不可能是2 的倍数。8/2 =4 4/2 =2 2/2 =1。最后肯定是得1。7%2 !=0不能被2整除肯定是不行的。[代码]public class S转载 2015-07-06 21:43:07 · 660 阅读 · 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 · 319 阅读 · 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 · 355 阅读 · 0 评论 -
【Leetcode】Valid Sudoku
【题目】Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A parti转载 2015-07-05 10:37:08 · 376 阅读 · 0 评论 -
【Leetcode】Reverse Integer
【题目】Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus po转载 2015-07-12 10:38:46 · 373 阅读 · 0 评论 -
【Leetcode】String to Integer(ATOI)
【题目】Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possibl转载 2015-07-12 17:03:52 · 380 阅读 · 0 评论 -
【Leetcode】Delete Node in a LinkedList
【题目】Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node转载 2015-07-26 16:47:59 · 291 阅读 · 0 评论 -
【leetcode】Rotate Array
【题目】Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note:Try to come up as many solut翻译 2015-07-02 23:21:57 · 279 阅读 · 0 评论 -
【Leetcode】Factorial Trailing Zeroes
【题目】Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.Credits:Special thanks to @ts for adding this problem an转载 2015-07-04 22:55:21 · 361 阅读 · 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 · 473 阅读 · 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 · 312 阅读 · 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 · 287 阅读 · 0 评论 -
【Leetcode】Lowest Common Ancestor of a Binary Search Tree
【题目】Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defi转载 2015-07-20 21:05:28 · 327 阅读 · 0 评论 -
[Leetcode]Remove duplicates
[timu]Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with转载 2015-09-02 08:10:54 · 290 阅读 · 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 · 337 阅读 · 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 · 277 阅读 · 0 评论 -
【Leetcode】Largest Ractangular
【题目】Largest Rectangle in Histogram Total Accepted: 42804 Total Submissions: 188970My SubmissionsQuestion Solution Given n non-negative integers representing the histogram转载 2015-09-02 11:02:12 · 284 阅读 · 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 · 260 阅读 · 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 · 399 阅读 · 0 评论 -
【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal
【题目】Given preorder and inorder traversal of a tree, construct the binary tree.【思路】Hint:A good way to attempt this question is to work backwards. Approach this question by drawing a binary转载 2015-09-03 02:13:50 · 527 阅读 · 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 · 311 阅读 · 0 评论 -
【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 · 392 阅读 · 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 · 279 阅读 · 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 · 223 阅读 · 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 · 249 阅读 · 0 评论 -
【Leetcode】Implement Queue using Stacks
【题目】Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front elem转载 2015-07-20 22:20:09 · 328 阅读 · 0 评论 -
【leetcode】Count Primes
【题目】Description:Count the number of prime numbers less than a non-negative number, n.Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.Hint:Let's转载 2015-07-21 09:29:39 · 300 阅读 · 0 评论 -
【Leetcode】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 sta转载 2015-07-21 21:00:23 · 264 阅读 · 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 · 269 阅读 · 0 评论