leetcode
文章平均质量分 70
ljffdream
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
[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 · 735 阅读 · 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 · 354 阅读 · 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 · 394 阅读 · 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 · 314 阅读 · 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 · 295 阅读 · 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 · 409 阅读 · 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 · 338 阅读 · 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 · 406 阅读 · 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 · 399 阅读 · 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 · 600 阅读 · 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 · 339 阅读 · 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 · 531 阅读 · 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 · 382 阅读 · 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 · 376 阅读 · 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 · 389 阅读 · 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 · 520 阅读 · 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 · 420 阅读 · 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 · 332 阅读 · 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 · 344 阅读 · 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 · 445 阅读 · 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 · 417 阅读 · 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 · 430 阅读 · 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 · 392 阅读 · 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 · 333 阅读 · 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 · 324 阅读 · 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 · 418 阅读 · 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 · 474 阅读 · 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 · 365 阅读 · 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 · 605 阅读 · 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 · 491 阅读 · 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 · 362 阅读 · 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 · 364 阅读 · 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 · 322 阅读 · 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 · 420 阅读 · 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 · 345 阅读 · 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 · 328 阅读 · 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 · 605 阅读 · 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 · 300 阅读 · 0 评论 -
【Leetcode】maximum Rangtangle in 2D matrix
【题目】Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.【思路】只能说不明觉厉:The DP solution proceeds row by row, starting f转载 2015-09-02 11:10:29 · 372 阅读 · 0 评论 -
【Leetcode】Merge intervals
【题目】Given a collection ofintervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].【思路】思路就是,先对进来的这一个list 的inteval进行比较,排序,排序的时候转载 2015-08-31 22:31:50 · 303 阅读 · 0 评论
分享