
leetcode
文章平均质量分 55
yuanhisn
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode 31 - Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible...原创 2014-12-27 07:23:37 · 136 阅读 · 0 评论 -
LeetCode - Serialize a binary tree
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called ‘serialization’ and reading back from the file to reconstruct the exact same binar...原创 2014-12-30 11:12:19 · 106 阅读 · 0 评论 -
LeetCode - Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Return 1 s...原创 2015-01-05 14:15:10 · 117 阅读 · 0 评论 -
LeetCode - Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()...原创 2015-01-07 08:36:31 · 82 阅读 · 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.这个要用到数学知识了。参见Wikipedia-Trailing Zeroes。The number of trailing zero...原创 2015-01-07 10:33:29 · 203 阅读 · 0 评论 -
LeetCode 142 - Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space? 算法参见Wekipedia.1. 假设圈的周长λ,相遇的时候乌龟走的路程:μ + k,...原创 2015-01-07 13:37:43 · 81 阅读 · 0 评论 -
LeetCode - First Missing Positive
Given an unsorted integer 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 uses constant space...原创 2015-01-10 00:09:14 · 69 阅读 · 0 评论 -
LeetCode - Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3]...原创 2015-01-10 07:22:25 · 69 阅读 · 0 评论 -
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 a...原创 2015-01-10 09:42:36 · 82 阅读 · 0 评论 -
LeetCode - Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in...原创 2015-01-13 03:48:17 · 102 阅读 · 0 评论 -
LeetCode - Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),...原创 2015-01-14 03:05:33 · 82 阅读 · 0 评论 -
LeetCode - Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You may not ...原创 2015-01-14 03:08:45 · 176 阅读 · 0 评论 -
LeetCode - Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ ...原创 2015-01-14 04:20:45 · 92 阅读 · 0 评论 -
LeetCode - Word Break
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", "c...原创 2015-01-15 07:36:37 · 73 阅读 · 0 评论 -
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, givens = "cats...原创 2015-01-15 07:38:42 · 62 阅读 · 0 评论 -
LeetCode 166 - Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.F...原创 2015-01-15 13:49:18 · 79 阅读 · 0 评论 -
LeetCode - LRU Cache
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 ...原创 2015-01-16 08:40:55 · 71 阅读 · 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. Solution1:自顶向下构建BST。不足之处是每次构建节点都要遍历寻找中间节点。public TreeNode sortedListToBST(ListNo...原创 2015-01-18 14:03:16 · 110 阅读 · 0 评论 -
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 devise a c...原创 2015-01-18 15:11:23 · 72 阅读 · 0 评论 -
LeetCode - Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3 Return 6. a...原创 2015-01-18 16:40:26 · 87 阅读 · 0 评论 -
LeetCode - Decode Ways
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 t...原创 2015-01-18 17:20:34 · 97 阅读 · 0 评论 -
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...原创 2015-01-19 12:22:46 · 86 阅读 · 0 评论 -
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 false....原创 2015-01-20 00:38:44 · 82 阅读 · 0 评论 -
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-21 15:26:40 · 100 阅读 · 0 评论 -
LeetCode 19 - Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node fro...原创 2015-01-22 06:44:32 · 75 阅读 · 0 评论 -
LeetCode 134 - 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 station i to its...原创 2015-02-05 05:40:06 · 88 阅读 · 0 评论 -
LeetCode - Binary Tree Traversal
Pre-orderpreorder(node) if node == null then return visit(node) preorder(node.left) preorder(node.right)iterativePreorder(node) parentStack = empty stack while (not...原创 2015-02-06 07:55:58 · 74 阅读 · 0 评论 -
LeetCode 108 - Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.public TreeNode sortedArrayToBST(int[] num) { if(num.length==0) return null; return toBST...原创 2015-02-06 14:48:01 · 86 阅读 · 0 评论 -
LeetCode 109 - 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.private ListNode h;public TreeNode sortedListToBST(ListNode head) { if(head == n...原创 2015-02-06 14:50:06 · 80 阅读 · 0 评论 -
LeetCode 44 - 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 cov...原创 2015-02-14 23:57:06 · 90 阅读 · 0 评论 -
LeetCode 138 - Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.Solution 1:这种方法很直观,先遍历链表一次,拷贝n...原创 2015-02-15 00:35:14 · 99 阅读 · 0 评论 -
LeetCode 17 - Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit stri...原创 2015-02-15 01:28:22 · 82 阅读 · 0 评论 -
LeetCode 187 - Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Wri...原创 2015-02-15 06:28:46 · 100 阅读 · 0 评论 -
LeetCode 36 - 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 partially filled sudok...原创 2015-02-16 00:31:06 · 80 阅读 · 0 评论 -
LeetCode 37 - Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudoku puzzle....原创 2015-02-16 00:32:50 · 97 阅读 · 0 评论 -
LeetCode 11 - Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two...原创 2015-02-16 05:07:59 · 86 阅读 · 0 评论 -
LeetCode 153 - Find Minimum in Rotated Sorted Array
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 exists in the ar...原创 2015-02-21 11:21:45 · 82 阅读 · 0 评论 -
LeetCode 54 - 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 ]]...原创 2015-02-21 14:16:00 · 85 阅读 · 0 评论 -
LeetCode 9 - Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.public boolean isPalindrome(int x) { int y = 0; while(x >= y) { y = y * 10 + x % 10; if(x =...原创 2015-02-24 01:59:11 · 71 阅读 · 0 评论 -
LeetCode 65 - Valid Number
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statemen原创 2015-02-24 01:59:34 · 75 阅读 · 0 评论