LeetCode
yao_hust_iAC
这个作者很懒,什么都没留下…
展开
-
LeetCode:Longest Palindromic Substring
题目描述: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.解题思路:原创 2014-11-27 13:02:50 · 620 阅读 · 0 评论 -
LeetCode:Rotate Image
题目描述:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思路:先将行顺序reverse,然后再对每个处于下三角区域的元素rotate。原创 2014-12-25 10:50:45 · 511 阅读 · 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原创 2014-12-25 10:11:39 · 530 阅读 · 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 inte原创 2014-12-26 09:55:07 · 527 阅读 · 0 评论 -
LeetCode:Minimum Path Sum
题目描述: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原创 2014-12-26 10:23:36 · 489 阅读 · 0 评论 -
LeetCode:Spiral Matrix II
题目描述:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8,原创 2014-12-26 11:36:27 · 550 阅读 · 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.Fi原创 2014-12-26 17:03:36 · 497 阅读 · 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],原创 2014-12-27 10:50:48 · 594 阅读 · 0 评论 -
LeetCode:Unique Paths II
题目描述:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectivel原创 2014-12-29 09:44:30 · 640 阅读 · 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原创 2014-12-29 10:28:40 · 610 阅读 · 0 评论 -
LeetCode: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 majori原创 2014-12-24 09:46:22 · 579 阅读 · 0 评论 -
LeetCode:Find Peak Element
题目描述:A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple原创 2014-12-24 12:05:33 · 632 阅读 · 0 评论 -
LeetCode: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原创 2014-11-06 11:24:36 · 593 阅读 · 0 评论 -
LeetCode:Palindrome Number
题目描述:Determine whether an integer is a palindrome. Do this without extra space.代码:bool Solution::isPalindrome(int x){ int a = x; int b = 0; while(a > 0) { b = b *原创 2014-11-05 20:12:18 · 600 阅读 · 0 评论 -
LeetCode:Valid Palindrome
题目描述:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car原创 2014-11-05 20:47:49 · 537 阅读 · 0 评论 -
LeetCode:Binary Tree Zigzag Level Order Traversal
题目描述:Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Gi原创 2014-12-01 09:33:30 · 573 阅读 · 0 评论 -
LeetCode:Evaluate Reverse Polish Notation
题目描述:Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples:原创 2014-11-25 09:50:19 · 601 阅读 · 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原创 2014-11-25 11:13:06 · 693 阅读 · 0 评论 -
LeetCode:Pow(x, n)
题目描述:Implement pow(x, n).代码:double solution::pow(double x,int n){ if(n == 0) return 1; if(n == 1) return x; if(n < 0) { n = n * -1; x = 1 / x; } return n%2==0?pow(x*x,n/2):x*p原创 2015-01-07 12:20:42 · 531 阅读 · 0 评论 -
LeetCode:Sqrt(x)
题目描述:Implement int sqrt(int x).Compute and return the square root of x.思路分析:采用二分查找的思想。当未找到mid=x/mid时,若mid>x/mid,则表示mid-1为平方值最接近但不超过x的值,即结果为mid-1。若mid代码: if(x == 0 || x == 1)原创 2015-01-07 19:02:00 · 581 阅读 · 0 评论 -
LeetCode: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 pare原创 2015-01-08 11:32:13 · 1057 阅读 · 1 评论 -
LeetCode:Unique Binary Search Trees II
题目描述:Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1原创 2014-12-30 10:26:42 · 516 阅读 · 0 评论 -
LeetCode:Jump Game
题目描述:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.原创 2015-01-05 10:49:31 · 632 阅读 · 0 评论 -
LeetCode:Combination Sum
题目描述:Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unl原创 2015-01-05 11:26:25 · 678 阅读 · 0 评论 -
LeetCode:Reverse Bits
题目描述:Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary a原创 2015-05-14 15:59:54 · 538 阅读 · 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].思路分析:将数组分为左右两部分,先将左右两部分各自翻转,最后再将整个原创 2015-05-14 16:29:28 · 1297 阅读 · 0 评论 -
Pascal's Triangle II
题目描述:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].思路分析:依次计算每一行的值,把它们存在同一个数组里。代码:class Solution{ public: vector原创 2015-05-27 15:27:18 · 994 阅读 · 0 评论 -
LeetCode:Word Search
题目描述: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 horizontally or原创 2015-05-18 17:01:00 · 642 阅读 · 0 评论 -
LeetCode:Candy
题目描述:There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have原创 2015-05-19 15:30:44 · 595 阅读 · 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 = "aadbb原创 2015-05-19 16:38:49 · 523 阅读 · 0 评论 -
LeetCode: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,原创 2015-05-19 18:21:14 · 506 阅读 · 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 adj原创 2015-05-14 15:31:16 · 575 阅读 · 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-05-25 16:37:24 · 536 阅读 · 0 评论 -
LeetCode:Bitwise AND of Numbers Range
题目描述:Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.代码:class Solution{ public: int rangeBitwiseAnd(int m,int n) { int i = 0; while(m原创 2015-05-12 15:25:21 · 791 阅读 · 0 评论 -
LeetCode:Number of Islands
题目描述:Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. Y原创 2015-05-12 16:14:35 · 892 阅读 · 0 评论 -
LeetCode:Construct Binary Tree from Preorder and Inorder Traversal
题目描述:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.解题思路:先序遍历的第一个元素为根元素,在中序遍历序列中找到根元素的位置。中原创 2015-01-06 10:56:26 · 564 阅读 · 0 评论 -
Intersection of Two Linked Lists
题目描述:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2015-01-06 09:30:14 · 917 阅读 · 0 评论 -
LeetCode:Longest Common Prefix
题目描述:Write a function to find the longest common prefix string amongst an array of strings.代码:string Solution::longestCommonPrefix(vector &strs){ string result = ""; if(strs.size(原创 2015-01-06 10:04:00 · 574 阅读 · 0 评论 -
LeetCode:Convert Sorted Array to Binary Search Tree
题目描述:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:采用类似折半查找的方式找到当前根节点,则当前根节点左边的属于它的左子树部分,当前根节点右边的属于它的右子树部分。再采用同样的方法,递归地对当前根节点的左右子树做相同的处理。原创 2014-10-30 17:03:16 · 560 阅读 · 0 评论 -
LeetCode:Search Insert Position
问题描述:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in原创 2014-10-28 10:52:38 · 535 阅读 · 0 评论