- 博客(68)
- 资源 (1)
- 收藏
- 关注
原创 [Leetcode] Max Tree Sum
不多说了~/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */c
2013-07-16 16:27:39 450
原创 [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. * struct ListNode { * int va
2013-03-11 03:32:38 414
原创 [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?剥洋葱大法~注意有时候算层数时候要记得处理余下的一层,这题不需要。例如size=5,一共有两个外层
2013-03-11 00:56:00 644
原创 [Leetcode] Pow(x,n)
Implement pow(x, n).class Solution {public: double pow(double x, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n==0) return 1
2013-03-10 23:35:49 445
原创 [Leetcode] Maximum Subarrary
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
2013-03-10 14:32:55 474
原创 [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, 9 ]]
2013-03-10 13:28:39 391
原创 [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.class Solution{public: ListNo
2013-03-10 10:19:50 385
原创 [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:Given binar
2013-03-10 09:16:32 388
原创 [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, t
2013-03-10 08:26:40 367
原创 [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 vertically
2013-03-10 05:13:01 386
原创 [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 width
2013-03-10 03:36:40 541
原创 [Leetcode] reverse nodes in K-Group
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 end should remain as it is
2013-03-10 01:32:30 433
原创 [Leetcode] Implement strStr()
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.终于鼓起勇气把KMP做了。推荐不熟悉KMP的童鞋看这篇帖子:http://www.matrix67.com/blog/archive
2013-03-09 15:11:50 514
原创 [Leetcode] Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.牛顿迭代class Solution {public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT w
2013-03-08 13:02:44 416
原创 [Leetcode] Subsets II
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 set must not contain d
2013-03-08 04:11:02 580
原创 [Leetcode] Permutations II
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], [1,2,1], and [2,1,1
2013-03-08 03:29:28 374
原创 [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].注意for循环的其实条件。
2013-03-08 02:24:35 348
原创 [Leetcode] Recover Binary Search Tree
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].空间为O(1)的思想是容易的,进行中序遍历,每次记
2013-03-08 01:56:01 374
原创 [Leetcode] Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Recursionclass So
2013-03-08 00:34:36 431
原创 [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.树根是后序遍历的最后一个元素。/** * Definition for binary tree
2013-03-08 00:03:58 448
原创 [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 20
2013-03-07 23:33:10 366
原创 [Leetcode] Maximum Subarray
Complexity: O(n).class Solution {public: int maxSubArray(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n==
2013-03-07 03:03:06 386
原创 [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, n satisfy the following condi
2013-03-06 09:49:38 456
原创 [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 to x.You should preserve the original relative order of the nodes in each
2013-03-06 04:14:44 430
原创 [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, 0). F
2013-03-06 02:13:57 475
原创 [Leetcode] Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr
2013-03-04 12:54:42 272
原创 [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 or right
2013-03-04 11:05:09 207
原创 [Leetcode] Unique BST 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
2013-03-04 08:57:54 331
原创 [Leetcode] Unique BST I
Given 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 unique BST's. 1 3 3 2 1
2013-03-04 06:25:21 187
原创 [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 = "aadbbbaccc",
2013-03-04 05:26:22 154
原创 [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 (can be n
2013-03-04 03:17:32 220
原创 [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 [1,
2013-03-04 01:46:18 199
原创 [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,3,2,1,2,
2013-03-04 01:42:30 164
原创 [Leetcode] Convert Sorted List to BST
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.起初觉得这道题和 Convert Sorted Array to BST 是一样的,但是如果要用那个二分法的算法,则需要将 list 先处理成 array。这样一来,
2013-03-03 04:11:52 331
原创 [Leetcode] Convert Sorted Array to BST
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.简单,二分法。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left
2013-03-03 03:59:38 224
原创 [Leetcode] Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without
2013-03-03 03:32:16 171
原创 [Leetcode] Populating Next Right Pointers in Each Node ii
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constan
2013-03-02 21:32:11 136
原创 [Leetcode] Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in t
2013-03-02 12:31:06 148
原创 [Leetcode] Remove Duplicates from Sorted Arrary II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?善用返回值!class Solution {public: int removeDuplicates(int A[], int n) { // Start typing your C/C++
2013-03-02 10:32:37 167
原创 [Leetcode] 2 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
2013-03-02 05:20:34 212
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人