- 博客(48)
- 资源 (2)
- 收藏
- 关注
原创 LeetCode 96: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 \
2015-12-31 16:35:13 3455
原创 LeetCode 257:Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]Cre
2015-12-31 15:39:41 2135
原创 LeetCode 129: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 the tota
2015-12-31 14:30:09 1982
原创 LeetCode 117: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 constant
2015-12-31 00:43:12 1952 2
原创 LeetCode 116: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 right node.
2015-12-30 23:28:03 1793
原创 LeetCode 124:Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The
2015-12-30 22:36:02 2371
原创 LeetCode 113: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 / \
2015-12-28 17:44:48 1649
原创 LeetCode 111: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.//递归方法求二叉树的最小深度 //注意判断树的深度应该到叶子节点
2015-12-28 16:16:40 1453
原创 LeetCode 99: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 devis
2015-12-28 15:37:36 3426
原创 LeetCode 103: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
2015-12-26 21:23:09 1736 4
原创 LeetCode 145: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: Recursive solut
2015-12-26 19:59:50 1896 2
原创 LeetCode 94: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].Note: Recursive solutio
2015-12-26 17:40:18 2431 2
原创 LeetCode 144: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: Recursive soluti
2015-12-25 17:11:52 2077 4
原创 LeetCode 112: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
2015-12-22 12:50:32 1565 1
原创 LeetCode119: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]//方法一:利用两个vector,一个保存当前层的数,一个保存前面所有层的数//空间复杂度较高class Solution {public: vector getRo
2015-12-18 17:03:04 707
原创 LeetCode 172:Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.//题目描述:给定一个整数n,返回n!(n的阶乘)数字中的后缀0的个数。//方法一:先求得n的阶乘,然后计算末尾0的个数,这种方法当n比较大是,n!会溢出class Solution {public: int trailingZeroes(int n) {
2015-12-18 14:28:10 1193
原创 LeetCode118:Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]Subscribe to see
2015-12-18 09:37:35 987
原创 LeetCode 66:Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.//题意:一个整数按位存储于一个int数
2015-12-17 00:48:03 3748
原创 LeetCode 213:House Robber II
Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time
2015-12-15 21:35:30 3089
原创 LeetCode 198:House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house
2015-12-15 20:09:07 2952 1
原创 LeetCode 102: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
2015-12-15 12:33:53 900
原创 LeetCode 107: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,#,#,15,7},
2015-12-15 12:27:55 1127
原创 LeetCode 101:Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f
2015-12-15 00:34:07 1845
原创 LeetCode 110: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 diffe
2015-12-14 22:39:54 2431 2
原创 LeetCode 231:Power of Two
Given an integer, write a function to determine if it is a power of two.//题目要求:求一个数是否是2的幂次方//解题方法://方法一:如果某个值是2的幂次方所得,其对应二进制则是最高位为1,其余位为0.//n-1则相反,除了最高位,其余比特位均为1,则我们可以判断n&(n-1)是否等于0来判断n是否是2的幂
2015-12-13 20:11:40 1722 2
原创 LeetCode 232:Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.em
2015-12-13 19:31:59 1876
原创 LeetCode 202:Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares
2015-12-13 00:45:52 1335
原创 LeetCode 263:Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc
2015-12-12 23:20:24 2866
原创 LeetCode 83: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.Subscribe to see wh
2015-12-11 17:11:56 1433
原创 LeetCode 70: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?//设f(n)表示爬n阶楼梯的不同方法,有两种选择:/
2015-12-11 16:34:34 919
原创 LeetCode 206:Reverse Linked List
Reverse a singly linked list.一、题目描述 翻转单链表二、解题思路利用两个结点指针和一个中间结点指针temp(用来记录当前结点的下一个节点的位置),分别指向当前结点和前一个结点,每次循环让当前结点的指针域指向前一个结点即可,翻转结束后,记得将最后一个节点的链域置为空。class Solution {public: List
2015-12-11 11:21:42 660
原创 LeetCode 191:Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 000000
2015-12-11 09:38:56 962
原创 LeetCode 235:Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw
2015-12-11 00:31:53 1334
原创 LeetCode 169:Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element
2015-12-10 23:47:39 1104
原创 LeetCode 171:Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ...
2015-12-07 22:31:09 504
原创 LeetCode 217:Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element
2015-12-07 16:46:44 444
原创 LeetCode 242:Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.//题目要求:给定字符串s和t,编写
2015-12-07 15:54:14 492
原创 C++中sort()函数的用法
C++中的sort()函数:1.sort(p, p + n, cmp); p为待排序数组的首地址,p+ n为待排序数组的最后一个数据的地址。cmp为自定义的排序规则函数,可省略;2.sort()函数默认是为升序排列;3.头文件:#include4.sort(buffer, buffer + n, cmp),其中的cmp可以自定义,使得sort降序排序:bool cmp(int
2015-12-07 15:48:42 908
原创 LeetCode 226:Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by this original tweet by Max Howe
2015-12-07 14:42:47 450
原创 LeetCode 100:Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value./** * Def
2015-12-07 11:16:08 685
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人