
leetcode
文章平均质量分 76
longhopefor
自强不息
展开
-
Leetcode--Permutation Sequence
Problem Description: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):原创 2014-08-30 17:05:19 · 904 阅读 · 0 评论 -
leetcode--Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate word m原创 2014-04-16 19:22:56 · 808 阅读 · 0 评论 -
Leetcode--Pow(x, n)
Problem Description:Implement pow(x, n).分析:题目意思很简单,要求计算x的n次幂,其中x给的是double类型,n需要考虑负数的情况,利用二分的思想每次将n减半,递归计算可以得到最终结果,其中一些细节需要注意,具体的实现代码如下:class Solution {public: bool isequal(double a,doubl原创 2014-08-07 15:41:22 · 755 阅读 · 0 评论 -
Leetcode--Add Two Numbers
Problem Description:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbe原创 2014-07-31 20:19:10 · 828 阅读 · 0 评论 -
Leetcode--Jump Game
Problem Description: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 tha原创 2014-08-07 15:32:44 · 703 阅读 · 0 评论 -
Leetcode--Remove Duplicates from Sorted Array
Problem Description:Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you原创 2014-07-31 21:13:50 · 604 阅读 · 0 评论 -
Leetcode--Merge Intervals
Problem Description:Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].分析:按照要求将区间合并,首先将按照按起点排序,然后原创 2014-08-07 16:58:12 · 870 阅读 · 0 评论 -
Leetcode--Roman to Integer
Problem Description:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.分析:题目的意思是将罗马数字转化成整数,首先是在网上找到关于罗马数字表示法的规则如下: 1、计数方法:① 罗马数字就原创 2014-08-05 20:08:07 · 723 阅读 · 0 评论 -
Leetcode--Next Permutation
Problem Description:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rea原创 2014-08-05 21:01:43 · 683 阅读 · 0 评论 -
Leetcode--Reverse Nodes in k-Group
Problem Description:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the原创 2014-07-11 15:32:00 · 763 阅读 · 0 评论 -
Leetcode--Generate Parentheses
Problem Description: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:"((()))", "(()())",原创 2014-08-08 21:15:31 · 775 阅读 · 0 评论 -
Leetcode--Recover Binary Search Tree
Problem Description: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 straigh原创 2014-08-08 20:44:33 · 1021 阅读 · 0 评论 -
Leetcode--N-Queens
Problem Description: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 solution原创 2014-08-08 10:50:25 · 808 阅读 · 0 评论 -
Leetcode--Flatten Binary Tree to Linked List
Problem Description:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree sho原创 2014-08-15 10:20:17 · 798 阅读 · 0 评论 -
Leetcode--Palindrome Partitioning II
Problem Description: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原创 2014-09-11 20:06:39 · 943 阅读 · 0 评论 -
Leetcode--Reorder List
Problem Description:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given原创 2014-08-15 21:57:52 · 647 阅读 · 0 评论 -
Leetcode--Sort List
Problem Description:Sort a linked list in O(n log n) time using constant space complexity.分析:对链表进行排序,思考排序算法时间复杂度为O(nlogn)的只有归并,快排和堆排序,应用到链表上的归并比较合适,这里利用快慢指针找到链表的中间节点,然后分别对两边递归归并排好序后将两边归并即可得到最终原创 2014-08-13 21:48:47 · 645 阅读 · 0 评论 -
leetcode--Reverse Words in a String
Problem Description:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarificat原创 2014-08-13 20:42:24 · 641 阅读 · 0 评论 -
Leetcode--Subsets II
Problem Description:Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution se原创 2014-09-09 21:05:21 · 962 阅读 · 0 评论 -
Leetcode--permutations II
Problem Description:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2原创 2014-09-09 13:02:12 · 1109 阅读 · 0 评论 -
Flatten Binary Tree to Linked List
Problem Description:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree s原创 2014-09-05 19:21:38 · 823 阅读 · 0 评论 -
Leetcode--Convert Sorted List to Binary Search Tree
Problem Description:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.分析:很容易想到的一种解法是将链表中所有的元素保存到数组中,然后每次取中间值进行构造,时间复杂度为O(n),空间复杂度为O(n)。具体原创 2014-09-05 17:23:46 · 807 阅读 · 0 评论 -
Leetcode--Divide Two Integers
Problem Description:Divide two integers without using multiplication, division and mod operator.分析:题目意思很容易理解,就是不用乘除法和模运算求来做除法,很容易想到的一个方法是一直做减法,然后计数,但是提交之后显示超时,在网上找到一种解法,利用位运算,意思是任何一个整数可以表示成以2的幂为原创 2014-08-04 20:43:31 · 817 阅读 · 0 评论 -
Leetcode--Anagrams
Problem Description:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.分析:题目要求输出找出所有在字符串数组中的变形词,变形词的意思是指单词由相同的字母构成,只是字母在单词中的顺序原创 2014-08-01 15:04:52 · 772 阅读 · 0 评论 -
leetcode--Permutations(打印所有排列)
Problem Description: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 [原创 2014-04-01 19:45:33 · 1151 阅读 · 0 评论 -
leetcode--Populating Next Right Pointers in Each Node
Problem Description:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to poin原创 2014-04-01 16:54:50 · 503 阅读 · 0 评论 -
Leetcode--Path Sum
Problem Description: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原创 2014-03-20 19:31:58 · 576 阅读 · 0 评论 -
Leetcode--Path Sum II
Problem Description: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,原创 2014-03-20 20:32:58 · 687 阅读 · 0 评论 -
leetcode--Combination Sum
Problem Description: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 ch原创 2014-04-01 14:59:02 · 657 阅读 · 0 评论 -
Leetcode--Sum Root to Leaf Numbers
Problem Description: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 nu原创 2014-03-18 14:32:05 · 546 阅读 · 0 评论 -
Leetcode--Sort List
Problem Description:Sort a linked list in O(n log n) time using constant space complexity.分析:我们知道数组和链表的最大不同就是:数组支持随机访问,而链表不支持,只能顺序访问。这就导致很多需要随机访问的排序算法在链表上应用效果不好,例如堆排序,经常需要计算并访问父/子节点的下标。这里利用归并排原创 2014-03-17 21:38:33 · 574 阅读 · 0 评论 -
Leetcode--3Sum
Problem Description: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:Elem原创 2014-03-12 19:43:25 · 530 阅读 · 0 评论 -
Leetcode--Combination Sum
Problem Description: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 ch原创 2014-03-12 16:08:06 · 559 阅读 · 0 评论 -
Leetcode--Search in Rotated Sorted Array(旋转数组的查找)
Problem Description: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. I原创 2014-03-26 18:34:18 · 697 阅读 · 0 评论 -
Leetcode--Word Break II
Problem Description: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.原创 2014-04-10 19:30:45 · 836 阅读 · 0 评论 -
Leetcode--Multiply Strings
Problem Description:Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.分析:两个string相乘原创 2014-08-01 15:35:08 · 716 阅读 · 0 评论 -
Leetcode--ZigZag Conversion
Problem Description:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)原创 2014-07-31 20:55:57 · 659 阅读 · 0 评论 -
Leetcode--Two Sum
Problem Description: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原创 2014-07-31 20:08:25 · 735 阅读 · 0 评论 -
Leetcode--3Sum
Problem Description: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:El原创 2014-07-07 19:26:58 · 933 阅读 · 0 评论 -
Leetcode--Combination Sum II
Problem Description: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 b原创 2014-07-26 10:00:09 · 827 阅读 · 0 评论