
LeetCode
文章平均质量分 73
LeetCode中题目的解答,希望在这里记录下学习的足迹。
vincent-xia
这个作者很懒,什么都没留下…
展开
-
LeetCode104:Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Show TagsShow Similar Proble原创 2015-08-13 23:25:13 · 724 阅读 · 0 评论 -
LeetCode218:The Skyline Problem
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as原创 2015-08-04 11:09:06 · 3712 阅读 · 0 评论 -
LeetCode146: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-08-02 10:05:04 · 1858 阅读 · 0 评论 -
LeetCode206:Reverse Linked List
Reverse a singly linked list.click to show more hints.Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?使用迭代和递归实现单链表的反转。迭代的方法之间在剑指offer上面原创 2015-07-30 11:22:27 · 2156 阅读 · 0 评论 -
LeetCode222:Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely fille原创 2015-07-29 17:17:00 · 1531 阅读 · 0 评论 -
LeetCode229:Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.借助于Majority Element中的Boyer Moore算法,可以在O(1)的原创 2015-07-29 15:13:11 · 1550 阅读 · 0 评论 -
LeetCode230:Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if the原创 2015-07-29 11:16:55 · 682 阅读 · 0 评论 -
LeetCode231:Power of Two
Given an integer, write a function to determine if it is a power of two.Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.这道题有好多种解法。原创 2015-07-29 10:14:21 · 976 阅读 · 1 评论 -
LeetCode236:Lowest Common Ancestor of a 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 two node原创 2015-07-27 15:57:21 · 648 阅读 · 0 评论 -
LeetCode234:Palindrome 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?给定一个单链表,判断它的元素是否是回文。要求在O(n)的时间复杂度和O(1)的空间复杂度内求解。如果没有时间复杂度的限制,可以直接将链表反转再比较就原创 2015-07-27 15:47:28 · 987 阅读 · 0 评论 -
LeetCode235: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 defined betw原创 2015-07-24 16:18:04 · 692 阅读 · 0 评论 -
LeetCode203:Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5Credits:Special thanks原创 2015-07-24 15:46:17 · 1882 阅读 · 0 评论 -
LeetCode237:Delete Node in a Linked List
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 with val原创 2015-07-24 15:22:47 · 752 阅读 · 0 评论 -
LeetCode227:Basic Calculator II
Implement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should trunca原创 2015-07-20 16:48:00 · 979 阅读 · 0 评论 -
LeetCode11: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). Fin原创 2015-07-20 16:46:46 · 1380 阅读 · 0 评论 -
LeetCode42:Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]原创 2015-07-20 16:22:02 · 742 阅读 · 0 评论 -
LeetCode238:Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements ofnums except nums[i].Solve it without division and in O(原创 2015-07-20 15:56:13 · 1254 阅读 · 0 评论 -
LeetCode224:Basic Calculator
Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty space原创 2015-07-15 16:04:08 · 1021 阅读 · 1 评论 -
LeetCode173: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() and hasN原创 2015-07-14 16:14:09 · 686 阅读 · 0 评论 -
LeetCode145:Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, return [3,2,1].Note: Recursive solution is trivial, could you do it iteratively?二叉原创 2015-07-14 11:21:42 · 720 阅读 · 0 评论 -
LeetCode144:Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, return [1,2,3].Note: Recursive solution is trivial, could you do it iteratively?先序遍原创 2015-07-14 10:29:27 · 658 阅读 · 0 评论 -
LeetCode94:Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, return [1,3,2].Note: Recursive solution is trivial, could you do it iteratively?二叉树的原创 2015-07-14 09:56:27 · 704 阅读 · 0 评论 -
LeetCode49:Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case.这道题是题目都没读懂,不知道Anagrams什么意思。Anagrams的意思是那些字符串的组成字符相同。比如说abc,那么abc这三个字符的所有排列都是Anagr原创 2015-07-14 09:10:45 · 750 阅读 · 0 评论 -
LeetCode3:Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “原创 2015-07-13 00:24:28 · 1054 阅读 · 0 评论 -
LeetCode20:Valid Parentheses
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are all valid原创 2015-07-11 00:02:15 · 957 阅读 · 0 评论 -
LeetCode155:Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x onto stack.pop() – Removes the element on top of the stack.top() – Get the t原创 2015-07-10 23:32:51 · 841 阅读 · 0 评论 -
LeetCode232: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 element.empty() –原创 2015-07-10 23:08:22 · 1194 阅读 · 0 评论 -
LeetCode225:Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() – Return whether原创 2015-07-10 22:46:38 · 1495 阅读 · 0 评论 -
LeetCode215: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, return 5.Note:原创 2015-07-09 21:36:41 · 2467 阅读 · 0 评论 -
LeetCode169:Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element always原创 2015-07-09 19:29:49 · 2909 阅读 · 1 评论 -
LeetCode93:Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given “25525511135”, return [“255.255.11.135”, “255.255.111.35”]. (O原创 2015-07-08 15:50:36 · 2616 阅读 · 1 评论 -
LeetCode90:Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate原创 2015-07-08 11:12:56 · 1781 阅读 · 0 评论 -
LeetCode60: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):1 “123” 2 “132” 3 “213”原创 2015-07-08 10:45:24 · 983 阅读 · 0 评论 -
LeetCode131: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 [原创 2015-07-07 17:51:57 · 2386 阅读 · 0 评论 -
LeetCode52:N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 和N-Queens 同样的解法。class Solution {public: int totalNQueens(int n原创 2015-07-07 17:02:46 · 877 阅读 · 0 评论 -
LeetCode17: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 string “2原创 2015-07-07 16:50:00 · 933 阅读 · 0 评论 -
LeetCode89: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 gray原创 2015-07-07 15:59:59 · 3041 阅读 · 0 评论 -
LeetCode22:Generate Parentheses
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-07-07 11:27:50 · 1617 阅读 · 0 评论 -
LeetCode216:Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers w原创 2015-07-06 21:36:01 · 1119 阅读 · 0 评论 -
LeetCode40:Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat原创 2015-07-06 21:17:58 · 1623 阅读 · 0 评论