
LeetCode代码思路
通过刷LeetCode学习C++和算法思路,提高代码量。该专栏按照LeetCode刷题自然顺序,排除一些奇怪的小众的题目,重在基础数据结构与算法的训练。(持续更新中...)
Never-guess
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
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: 1 \ 2原创 2017-12-08 22:21:17 · 305 阅读 · 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 / \原创 2017-12-07 14:45:35 · 433 阅读 · 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 and sum = 2原创 2017-12-06 13:34:01 · 377 阅读 · 1 评论 -
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.思路:递归。这个题目相比求最大深度有些巧妙,它通过判断有没有兄弟节点来计算离根节点最近原创 2017-12-05 16:49:48 · 416 阅读 · 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 never differ by原创 2017-12-04 17:10:15 · 495 阅读 · 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.For this problem, a height-balanced binary tree is defined as a binary tree in which the dep原创 2017-12-03 10:51:15 · 478 阅读 · 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.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the tw原创 2017-12-02 11:19:02 · 573 阅读 · 0 评论 -
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,null,null,15,...原创 2017-12-01 14:12:33 · 242 阅读 · 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.思路:这道题的思路基本和上一题一样,不同之处在于后序遍历根节点在最后面。/** * Definition for a bin原创 2017-11-30 15:24:53 · 425 阅读 · 0 评论 -
LeetCode--Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.思路: 这道题是树中比较有难度的题目,需要根据先序遍历和中序遍历来构造出树来。这道题看似毫无头绪,其实梳理一下还是有章可循的。原创 2017-11-29 20:07:35 · 489 阅读 · 0 评论 -
LeetCode--Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.思路:递归。二叉树最大深度经典结论,左右分别递归下去,每层深度加1,求最大值。/**原创 2017-11-23 11:04:46 · 409 阅读 · 0 评论 -
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 binary tr...原创 2017-11-22 11:39:45 · 397 阅读 · 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,null,null,15,7], 3 / \ 9 20 ...原创 2017-11-21 12:12:49 · 468 阅读 · 0 评论 -
LeetCode--Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the原创 2017-11-20 15:12:10 · 385 阅读 · 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 you devise a const原创 2017-11-16 10:28:35 · 533 阅读 · 0 评论 -
LeetCode--Same Tree
Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Example 1:Inp原创 2017-11-15 10:55:24 · 526 阅读 · 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. The right原创 2017-11-14 10:43:58 · 487 阅读 · 0 评论 -
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”, return false原创 2017-11-13 10:40:18 · 443 阅读 · 0 评论 -
LeetCode--Unique Binary Search Trees
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 \ /原创 2017-11-11 17:17:40 · 333 阅读 · 0 评论 -
LeetCode--Unique Binary Search Trees II
Given an integer 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原创 2017-11-12 17:02:50 · 488 阅读 · 0 评论 -
Leetcode--Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree [1,null,2,3], 1 \ 2 / 3 return [1,3,2].Note: Recursive solution is t原创 2017-11-10 12:40:24 · 271 阅读 · 0 评论 -
Leetcode--Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example: Given “25525511135”,return [“255.255.11.135”, “255.255.111.35”]. (Order does not原创 2017-11-09 21:44:10 · 254 阅读 · 0 评论 -
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 co原创 2017-11-08 18:22:03 · 425 阅读 · 0 评论 -
LeetCode--Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,2]原创 2017-11-04 11:08:48 · 657 阅读 · 0 评论 -
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 eat /原创 2017-10-23 15:43:51 · 447 阅读 · 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 to x.You should preserve the original relative order of the nodes in each of the原创 2017-10-20 11:20:15 · 259 阅读 · 0 评论 -
LeetCode--Maximal Rectangle
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.For example, given the following matrix:1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0原创 2017-10-19 10:39:53 · 336 阅读 · 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 width of each bar原创 2017-10-18 14:41:16 · 356 阅读 · 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->...原创 2017-10-17 14:41:17 · 520 阅读 · 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.思路:依次遍历下一个节点,如果找到重复的节点,则用后一个取代。/原创 2017-10-16 11:32:34 · 244 阅读 · 0 评论 -
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? Suppose an array sorted in ascending order is rotated at some pi原创 2017-10-15 14:45:19 · 292 阅读 · 0 评论 -
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 neig原创 2017-10-13 15:51:05 · 265 阅读 · 0 评论 -
LeetCode--Subsets
Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example, If nums = [1,2,3], a solution is:[ [3], [1], [2],原创 2017-10-12 10:54:58 · 286 阅读 · 0 评论 -
LeetCode--Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.For example, If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]思路原创 2017-10-11 11:12:19 · 259 阅读 · 0 评论 -
LeetCode--Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example, S = “ADOBECODEBANC” T = “ABC” Minimum window is “BANC”.Note:原创 2017-10-11 10:44:14 · 446 阅读 · 0 评论 -
LeetCode--Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1,原创 2017-09-28 11:15:51 · 277 阅读 · 0 评论 -
LeetCode--Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of each row is原创 2017-09-27 10:32:19 · 310 阅读 · 0 评论 -
LeetCode--Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up: Did you use extra space? A straight forward solution using O(mn) space原创 2017-09-26 12:02:46 · 217 阅读 · 0 评论 -
LeetCode--Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) In原创 2017-09-25 10:59:03 · 293 阅读 · 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?Note: Given n will be a positive inte原创 2017-09-23 10:41:33 · 274 阅读 · 0 评论