LeetCode
文章平均质量分 74
Allanxl
这个作者很懒,什么都没留下…
展开
-
LeetCode | Regular Expression Matching
题目:Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire原创 2013-09-22 19:50:13 · 1335 阅读 · 0 评论 -
LeetCode | 3Sum
题目:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a原创 2013-09-22 14:10:03 · 1737 阅读 · 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原创 2013-09-21 21:51:57 · 1961 阅读 · 6 评论 -
LeetCode | Best Time to Buy and Sell Stock II
题目: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 as many transactions as you like原创 2013-09-21 20:32:14 · 1493 阅读 · 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原创 2013-09-21 20:27:36 · 1313 阅读 · 1 评论 -
LeetCode | Length of Last Word
题目:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note:原创 2013-09-19 23:44:42 · 1007 阅读 · 0 评论 -
LeetCode | Climbing Stairs
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?思路:利用DP的方法,原创 2013-09-19 23:35:32 · 9049 阅读 · 8 评论 -
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,原创 2013-09-18 20:27:34 · 1950 阅读 · 0 评论 -
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.思路:原创 2013-09-18 19:37:20 · 1549 阅读 · 0 评论 -
LeetCode | Path Sum II
题目: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原创 2013-09-17 22:15:10 · 1606 阅读 · 0 评论 -
LeetCode | Merge Sorted Array
题目:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initia原创 2013-09-17 20:53:03 · 1019 阅读 · 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 t原创 2013-09-15 22:53:03 · 1008 阅读 · 0 评论 -
LeetCode | 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 from t原创 2013-09-14 22:00:52 · 1092 阅读 · 0 评论 -
LeetCode | Plus One
题目:Given a number represented as an array of digits, plus one to the number.思路:数组的遍历需要从右向左进行,然后结果存在临时数组中为从左向右。输出前遍历一半的数组将数组反序。代码:class Solution {public: vector plusOne(v原创 2013-09-14 17:41:09 · 3458 阅读 · 0 评论 -
LeetCode | Palindrome Number
题目:Determine whether an integer is a palindrome. Do this without extra space.思路:翻转数字并与原数字比较。代码:class Solution {public: bool isPalindrome(int x) { // Start typin原创 2013-09-22 16:20:14 · 951 阅读 · 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.思路:类似http://blog.csdn.net/lanxu_yy/article/details/11898523,不过需要利用链表的方式找原创 2013-09-22 16:00:57 · 1226 阅读 · 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.思路:自顶向下地构造平衡二叉树。代码:/** * Definition for binary tree * struct TreeNode {原创 2013-09-22 15:07:29 · 1382 阅读 · 0 评论 -
LeetCode | 4Sum
题目:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Not原创 2013-09-22 14:28:49 · 995 阅读 · 0 评论 -
LeetCode | 3Sum Closest
题目:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would原创 2013-09-22 14:23:37 · 1045 阅读 · 0 评论 -
LeetCode | Sqrt(x)
题目:Implement int sqrt(int x).Compute and return the square root of x.思路:利用二分法查找:第一种可能性是直接找到能够麻烦要求的数;第二种可能性是找到相邻的两个数,可以比较两个数哪一个离target更近,不过题目当中希望找的是更小的那个数。代码:class Solut原创 2013-09-22 11:24:35 · 3340 阅读 · 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 tox.You should preserve the original relative order of the nodes原创 2013-09-22 00:59:17 · 971 阅读 · 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原创 2013-09-21 23:51:37 · 1261 阅读 · 0 评论 -
LeetCode | Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.思路:首先找出所有字符串的最小长度。然后依次比较前几位在各个字符串中是否相同,若相同,则作为输出结果;否则,停止循环。代码:class Solution {publi原创 2013-09-19 19:23:46 · 1236 阅读 · 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 c原创 2013-09-19 18:39:29 · 990 阅读 · 0 评论 -
LeetCode | Binary Tree Level Order Traversal
题目:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9原创 2013-09-18 20:30:16 · 1219 阅读 · 0 评论 -
LeetCode | Path Sum
题目:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree原创 2013-09-17 22:05:09 · 1169 阅读 · 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原创 2013-09-17 21:34:00 · 938 阅读 · 1 评论 -
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 n原创 2013-09-15 22:50:21 · 1064 阅读 · 0 评论 -
LeetCode | Two Sum
题目:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the ta原创 2013-09-14 13:42:50 · 6601 阅读 · 7 评论 -
LeetCode | Reverse Linked List II
题目:Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m,原创 2013-09-15 20:12:28 · 1019 阅读 · 0 评论 -
LeetCode | Integer to Roman
题目:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.思路:基本思路是将整数按照10进制存到一个数组中,针对每个数根据>5,=5,代码:class Solution {原创 2013-09-15 10:31:40 · 3222 阅读 · 3 评论 -
LeetCode | 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 statement原创 2013-09-15 00:25:46 · 3766 阅读 · 0 评论 -
LeetCode | Merge k Sorted Lists
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.思路:类似外排序的算法,直接上代码吧!题目:/** * Definition for singly-linked list. * stru原创 2013-09-14 21:49:57 · 1013 阅读 · 0 评论 -
LeetCode | N-Queens
题目:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-qu原创 2013-12-10 13:41:21 · 1137 阅读 · 0 评论 -
LeetCode | 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 stat原创 2013-12-10 11:47:09 · 3574 阅读 · 1 评论 -
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.思路:首先计算出链表元素个数,然原创 2013-12-10 16:04:07 · 1987 阅读 · 0 评论 -
LeetCode | N-Queens II
题目:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.思路:与http://blog.csdn.net/lanxu_yy/article/deta原创 2013-12-11 09:35:14 · 1564 阅读 · 3 评论 -
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 v原创 2013-12-13 17:42:35 · 1113 阅读 · 0 评论 -
LeetCode | 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.原创 2013-12-14 00:13:02 · 1811 阅读 · 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 partia原创 2013-12-14 00:22:33 · 1231 阅读 · 0 评论