
LeetCode(题目详细分析)
文章平均质量分 65
Eleven_Adam
这个作者很懒,什么都没留下…
展开
-
LeetCode :: Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible o原创 2014-08-07 23:27:15 · 924 阅读 · 0 评论 -
Leetcode :: Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without u原创 2014-08-05 21:52:31 · 1151 阅读 · 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原创 2014-08-04 22:00:43 · 973 阅读 · 0 评论 -
LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]
1.Given an array where elements are sorted in ascending order, convert it to a height balanced BST.2.Given a singly linked list where elements are sorted in ascending order, convert it to a heig原创 2014-07-21 23:02:17 · 1702 阅读 · 0 评论 -
LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]
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:Given binary原创 2014-07-20 17:58:45 · 936 阅读 · 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 station i to原创 2014-07-19 22:50:26 · 822 阅读 · 0 评论 -
LeetCode :: Candy
class Solution {public: int candy(vector &ratings) { int m = ratings.size(); int sum = 0; vector candy(m); candy[0] = 1; for(int i = 1; i < m; i++原创 2014-07-19 16:10:00 · 664 阅读 · 0 评论 -
LeetCode :: Sum Root to Leaf Numbers [tree、dfs]
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 to原创 2014-07-17 22:24:29 · 869 阅读 · 0 评论 -
LeetCode详细分析 :: Recover Binary Search Tree [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 you devis原创 2014-07-16 22:41:31 · 916 阅读 · 0 评论 -
LeetCode :: Validate Binary Search Tree[详细分析]
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.Th原创 2014-07-10 23:57:39 · 1167 阅读 · 0 评论 -
LeetCode:: Unique Binary Search Trees[详细分析]
Unique Binary Search Trees My SubmissionsGiven n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 uniqu原创 2014-07-06 20:42:31 · 1419 阅读 · 0 评论 -
LeetCode :: Reorder List
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 {1,2,3,4}, reorder it t原创 2014-06-23 23:16:05 · 901 阅读 · 0 评论 -
LeetCode :: Insertion Sort List [详细分析]
Sort a linked list using insertion sort.仍然是一个非常简洁的题目,让我们用插入排序给链表排序;这里说到插入排序,可以来回顾一下, 最基本的入门排序算法,就是插入排序了;时间复杂度为n^2,最基本的插入排序是基于数组实现的,下面给出基于数组实现的插入排序,来体会一个插入排序的思想;以下仅为数组实现,不是解题代码,没兴趣可以跳过。vo原创 2014-06-23 00:05:02 · 1584 阅读 · 0 评论 -
LeetCode::Sort List 详细分析
Sort a linked list in O(n log n) time using constant space complexity.这道题目非常简短的一句话,给链表排序,看到nlogn,我们可以来简单复习一下排序。首先说一下这个nlogn的时间复杂度(根据决策树我们可以得出这个界限),是基于比较排序的最小上限,也就是说,对于没有一定范围情况的数据来说,最快的排序思路就是归并和快速排原创 2014-06-21 22:38:57 · 937 阅读 · 0 评论 -
LeetCode::Remove Duplicates from Sorted List II [详细分析]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1-原创 2014-06-19 22:13:27 · 1285 阅读 · 0 评论 -
LeetCode :: Reverse Words in a String
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".这是LeetCode最新的一道题目,涉及到string的处理,好像之前我都没随机到字符串处理的问题,不得不说C++的字符串处理比C方便太原创 2014-04-01 14:46:16 · 1206 阅读 · 0 评论 -
LeetCode :: Gray Code[正确解答,真正考察点剖析]
这道题目,一般google之会发现很多答案都很简短,代码也就几行,但是你能写到这个代码的前提是,你知道什么叫做格雷码,并且格雷码和二进制码之间的转换公式。可是我觉得这道题根本就不是一道考察你知识点是否全面的题目,就算之前不知道格雷码的知识,仍然是可以写出来的,具体参见我的分析和代码。原创 2014-03-30 21:25:40 · 2234 阅读 · 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 (size that is greater or equal to m + n) to hold additional elements from原创 2014-03-29 23:29:55 · 975 阅读 · 0 评论 -
LeetCode :: Linked List Cycle I and II
I.Given a linked list, determine if it has a cycle in it.II. Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Can you solve it without using extra s原创 2014-03-29 22:21:16 · 980 阅读 · 0 评论 -
LeetCode :: Maximum Subarray 详细分析
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] ha原创 2014-03-28 11:34:58 · 2071 阅读 · 0 评论 -
LeetCode :: Merge Two Sorted Lists 详细分析
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.合并两个排好序的链表,这里重新设置两个指针,l3、trail,一个指示新链表的头结点,另一个指示新链表的最末有原创 2014-03-25 16:56:11 · 878 阅读 · 0 评论 -
LeetCode :: Remove Duplicates from Sorted List[详细分析]
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.这道题目,可以和之前的LeetCode ::原创 2014-03-24 23:31:42 · 840 阅读 · 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 the end, the原创 2014-03-24 22:45:39 · 650 阅读 · 0 评论 -
LeetCode :: Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.很原创 2014-03-24 21:28:39 · 726 阅读 · 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 A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,原创 2014-03-24 21:11:02 · 681 阅读 · 0 评论 -
LeetCode :: Remove Duplicates from Sorted Array [详细分析]
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 must do this in place with原创 2014-03-24 17:12:22 · 724 阅读 · 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原创 2014-03-24 15:05:52 · 945 阅读 · 0 评论 -
LeetCode :: 3.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 and sum原创 2014-03-22 17:43:48 · 665 阅读 · 0 评论 -
LeetCode :: 2.Minimum Depth of Binary Tree [树类题目分析]
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.作为树分类的第二篇,寻找最浅的那片叶子。这里是一个后序遍历的过程。P原创 2014-03-21 23:09:08 · 759 阅读 · 0 评论 -
LeetCode :: 1.Balanced Binary Tree [树类题目分析]
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtreesof every node never diff原创 2014-03-21 21:38:06 · 1041 阅读 · 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 target, whe原创 2014-03-20 22:26:32 · 994 阅读 · 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-03-13 22:51:51 · 858 阅读 · 0 评论 -
LeetCode :: Palindrome
今天真是lucky,随机到的题目都比较简单。这里判断回文数字。Determine whether an integer is a palindrome. Do this without extra space.题目也是很简短,唯一值得注意的是,负数全部不是回文数字,一开始我没考虑到负数,所以没AC。这里用 long long 来存在取反的数字,来避免越界问题。class Solut原创 2014-03-13 17:01:57 · 662 阅读 · 0 评论 -
LeetCode :: Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321题目比较简单,给出的函数原型int reverse(int x);这里让我费解的地方是展开讨论的这句话:Did you notice that the reversed integer migh原创 2014-03-13 16:45:35 · 695 阅读 · 0 评论 -
LeetCode :: Pow(x, n)
题目要求很简洁,Implement pow(x, n),没了。函数的原型是这样的:double pow(double x, int n) ;一、这里最容易想到的,肯定是时间复杂度为O(N), 利用一个递归,我试了一下,果然和想的一样 TL。二、把pow分解为 pow(x, n / 2) ,T(N) = T(N / 2) + O (1),这里的时间复杂度利用master method,显而易见是O(原创 2014-03-13 16:28:24 · 990 阅读 · 0 评论 -
LeetCode 开篇
经历了一些人,一些事,深深地认识到自己的不足,感觉应该脚踏实地地去工作、去学习,冰冻三尺,非一日之寒。首先,先从Leetcode这样一个经典的网站开始吧,一天写一些,更一些,好像网上有很多题解,但是好多都写得不是那么清楚或者写的很晦涩,我想写得详细一点,把每题涉及的问题都详细的分析与阐释一下,算是留给自己复习用吧。加油吧,骚年。原创 2014-03-13 15:40:34 · 777 阅读 · 0 评论