leetcode
文章平均质量分 59
在风雨中奔跑
这个作者很懒,什么都没留下…
展开
-
[LeetCode]Lowest Common Ancestor of a Binary Search Tree
问题:给定一个二叉树,找到两个节点NA, NB的最近公共祖先(LCA)。比如对于下图,4 和 7 的 LCA 是6, 1和13的LCA 是 8。分析:我们这里先考虑一般的二叉树(BT),然后再考虑这个二叉树是二叉搜索树(BST)的情况。查找两个node的最早的公共祖先,分三种情况:1. 如果两个node在root的两边,那么最早的转载 2015-08-27 16:57:46 · 331 阅读 · 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 possible input ca原创 2015-09-24 12:45:45 · 325 阅读 · 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.题解:使用的是筛选法。publ原创 2015-09-24 09:35:15 · 268 阅读 · 0 评论 -
[LeetCode]First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the原创 2015-09-24 09:51:49 · 304 阅读 · 0 评论 -
[LeetCode]Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].Credits:Special thanks to @jianchao.li.fighte原创 2015-09-24 10:23:26 · 341 阅读 · 0 评论 -
[LeetCode]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-09-24 16:47:24 · 245 阅读 · 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 space. Y原创 2015-09-24 17:19:55 · 295 阅读 · 0 评论 -
[LeetCode]Permutations
Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].题解:使用的是递归方法:原创 2015-09-24 18:17:38 · 313 阅读 · 0 评论 -
[LeetCode]Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm shoul原创 2015-09-24 15:14:51 · 303 阅读 · 0 评论 -
[LeetCode]H-Index II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?Hint:Expected runtime complexity is in O(log n) and the input is sorted.原创 2015-09-25 21:09:17 · 335 阅读 · 0 评论 -
[LeetCode]Power of Three
Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?题解:给一个数,判断这个数是否为3的多少次方?3x = N => x = log3N =>原创 2016-04-09 20:03:26 · 294 阅读 · 0 评论 -
[LeetCode]Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], ther原创 2016-04-13 15:47:07 · 338 阅读 · 0 评论 -
[LeetCode]Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:Given nums =原创 2016-04-10 14:44:04 · 267 阅读 · 0 评论 -
[LeetCode]Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num = 5原创 2016-04-10 15:17:05 · 280 阅读 · 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.For原创 2016-04-27 10:45:44 · 318 阅读 · 0 评论 -
[LeetCode]Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2016-04-27 11:19:45 · 365 阅读 · 0 评论 -
[LeetCode]Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in原创 2016-04-11 18:51:31 · 272 阅读 · 0 评论 -
[LeetCode]House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour原创 2016-04-11 19:58:44 · 300 阅读 · 0 评论 -
[LeetCode]Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and co原创 2015-09-24 11:51:06 · 333 阅读 · 0 评论 -
[LeetCode]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?题解:获得链表一半的位置,再将其反转,最后2个半截的链表比较public boolean isPalindrome(ListNode head) {原创 2015-09-24 00:14:57 · 299 阅读 · 0 评论 -
[LeetCode94]Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Total Accepted: 80048 Total Submissions: 219796My SubmissionsQuestion Solution Given a binary tree, return the inorder traversal of its nodes' value原创 2015-08-31 22:45:02 · 426 阅读 · 0 评论 -
[LeetCode]Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each原创 2015-09-25 11:58:44 · 303 阅读 · 0 评论 -
[LeetCode]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-09-25 11:23:45 · 309 阅读 · 0 评论 -
[LeetCode]Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(m原创 2015-09-25 12:39:15 · 402 阅读 · 0 评论 -
[LeetCode]H-Index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.According to the definition of h-index on Wikipedia: "A原创 2015-09-25 20:43:23 · 333 阅读 · 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 reach the原创 2015-09-07 15:45:27 · 321 阅读 · 0 评论 -
[LeetCode]Search 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).You are given a target value to search. If found in the array retur原创 2015-09-28 16:59:11 · 394 阅读 · 0 评论 -
[LeetCode]Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota原创 2015-09-28 17:47:33 · 307 阅读 · 0 评论 -
[LeetCode]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 t原创 2015-09-27 10:30:12 · 524 阅读 · 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-09-28 18:03:29 · 304 阅读 · 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 nums = [1,1,1,2,2,3],Your function should return length = 5, with the first fi原创 2015-09-28 17:36:21 · 301 阅读 · 0 评论 -
[LeetCode]Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2015-09-27 09:41:01 · 310 阅读 · 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 integers原创 2015-09-14 18:59:37 · 279 阅读 · 0 评论 -
[LeetCode]Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},原创 2015-09-21 11:17:14 · 310 阅读 · 0 评论 -
[LeetCode]Plus One
1Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.题意:给一个数的数组形式(例如78 d原创 2015-09-21 14:00:47 · 284 阅读 · 0 评论 -
[LeetCode]House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2015-09-22 11:59:17 · 245 阅读 · 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 partially fille原创 2015-09-23 12:14:16 · 366 阅读 · 0 评论 -
[LeetCode] 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 wi原创 2016-04-11 21:34:10 · 288 阅读 · 0 评论