LeetCode
文章平均质量分 63
Nero_pf
这个作者很懒,什么都没留下…
展开
-
LeetCode | Longest Substring Without Repeating Characters
题目Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3原创 2013-07-09 21:35:35 · 410 阅读 · 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 tar原创 2013-07-10 16:17:47 · 566 阅读 · 0 评论 -
LeetCode | Max Points on a Line
题目Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.分析这题没什么巧妙解法,就是枚举。遍历每一个点,统计其它点与该点构成直线的斜率相同的个数。需要注意两点:1. 重复出现的点要特殊处理。2. 用float或double原创 2014-02-05 14:11:16 · 1120 阅读 · 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.分析大牛给出了5种解原创 2013-07-10 19:03:47 · 727 阅读 · 0 评论 -
LeetCode | Largest Rectangle in Histogram
题目Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where原创 2014-02-05 13:52:04 · 1055 阅读 · 0 评论 -
LeetCode | Sort List
题目Sort a linked list in O(n log n) time using constant space complexity.分析由于要求O(nlgn)的时间复杂度,自然就是归并排序、堆排序、快速排序;但是,还要求了O(1)空间复杂度。如果递归不算空间的话,可以采用递归的归并排序。如果使用非递归的归并排序,由于是单向链表,没法像数组那样直接指定元素位置原创 2014-02-06 15:50:01 · 1026 阅读 · 3 评论 -
LeetCode | Insertion Sort List
题目Sort a linked list using insertion sort.分析题目已经明确说了用插入排序,由于是链表,无法像数组那样从后往前依次比较插入,只能从前往后了。在链表首部添加一个哨兵可以稍微简化下代码。代码public class InsertionSortList { public class ListNode { int val; Li原创 2014-02-07 15:58:19 · 550 阅读 · 0 评论 -
LeetCode | LRU Cache
题目Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the原创 2014-02-07 17:40:04 · 1339 阅读 · 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原创 2014-02-19 21:10:08 · 369 阅读 · 0 评论 -
LeetCode | Longest Consecutive Sequence
题目Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is原创 2014-02-20 21:02:32 · 517 阅读 · 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" i原创 2014-02-22 10:02:48 · 507 阅读 · 0 评论 -
LeetCode | Word Ladder II
题目Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be changed at a timeEach intermediate word原创 2014-02-21 23:13:28 · 2281 阅读 · 0 评论 -
LeetCode | Binary Tree Preorder Traversal
题目Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursiv原创 2014-02-10 10:28:37 · 444 阅读 · 0 评论 -
LeetCode | Binary Tree Postorder Traversal
题目Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursi原创 2014-02-09 19:51:26 · 498 阅读 · 0 评论 -
LeetCode | Binary Tree Maximum Path Sum
题目Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3原创 2014-02-23 11:25:06 · 506 阅读 · 0 评论 -
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 (ie原创 2014-02-23 18:40:33 · 513 阅读 · 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}, reorde原创 2014-02-11 14:45:00 · 610 阅读 · 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 the原创 2014-02-23 18:19:24 · 820 阅读 · 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:原创 2014-02-23 19:11:44 · 670 阅读 · 0 评论 -
LeetCode | Triangle
题目Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,原创 2014-02-24 23:06:31 · 505 阅读 · 0 评论 -
LeetCode | Word Break II
题目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.For example, give原创 2014-02-12 23:26:06 · 1065 阅读 · 0 评论 -
LeetCode | 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].Note:Could you optimize your algorithm to use only O(k) extra space?分析题目原创 2014-02-25 15:54:09 · 466 阅读 · 0 评论 -
LeetCode | Populating Next Right Pointers in Each Node
题目Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next righ原创 2014-02-25 21:01:46 · 625 阅读 · 0 评论 -
LeetCode | Distinct Subsequences
题目Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (ca原创 2014-02-25 23:07:02 · 726 阅读 · 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-02-26 21:05:15 · 4581 阅读 · 1 评论 -
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原创 2014-02-26 22:33:24 · 549 阅读 · 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 stati原创 2014-02-16 11:00:29 · 673 阅读 · 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 wit原创 2014-02-14 23:44:13 · 605 阅读 · 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 at原创 2014-02-15 11:52:03 · 746 阅读 · 0 评论 -
LeetCode | Copy List with Random Pointer
题目A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.分析题目的难点在于随机指针的拷贝。如下两原创 2014-02-13 22:06:19 · 864 阅读 · 0 评论 -
LeetCode | 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.分析题目很简单,需要尽可能写得精炼。有递归(DF原创 2014-02-27 22:35:15 · 1871 阅读 · 0 评论 -
LeetCode | 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 subtrees of every node nev原创 2014-02-27 22:47:49 · 548 阅读 · 0 评论 -
LeetCode | Clone Graph
题目Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separato原创 2014-02-16 22:51:52 · 556 阅读 · 0 评论 -
LeetCode | Palindrome Partitioning II
题目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, given s = "aab",原创 2014-02-17 21:47:44 · 654 阅读 · 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.分析递归不断构造左右子树代码public class ConvertSortedListToBinarySearchTree { public Tree原创 2014-02-28 23:31:39 · 722 阅读 · 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'原创 2014-03-02 11:25:02 · 617 阅读 · 0 评论 -
LeetCode | Construct Binary Tree from Inorder and Postorder Traversal
题目Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.分析找到根节点,递归构造即可代码public class ConstructBina原创 2014-03-01 19:07:09 · 567 阅读 · 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 y原创 2014-03-02 10:58:36 · 564 阅读 · 0 评论 -
LeetCode | Decode Ways
题目A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the原创 2014-03-02 14:38:08 · 546 阅读 · 0 评论 -
LeetCode | Surrounded Regions
题目Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region .For example,X X X原创 2014-02-18 21:44:02 · 2524 阅读 · 1 评论