- 博客(76)
- 收藏
- 关注
原创 104. 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.Note:A leaf is a node with no ...
2019-03-28 10:13:01 158
原创 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,null,null,15,7], 3 / \ 9...
2019-01-23 04:31:07 164
原创 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 [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 ...
2019-01-23 03:46:36 182
原创 100. Same Tree
题目描述:给定两个二叉树,写一个函数来检查它们是否相同。如果两棵树在结构上相同并且节点具有相同的值,则认为它们是相同的。示例 1:输入 : 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3]输出: true示例 2:输入 : 1 1...
2018-03-28 11:34:33 203
原创 98. Validate Binary Search Tree
题目描述:给定一个二叉树,判断其是否是一个有效的二叉搜索树。一个二叉搜索树有如下定义:左子树只包含小于当前节点的数。右子树只包含大于当前节点的数。所有子树自身必须也是二叉搜索树。示例 1: 2 / \ 1 3二叉树[2,1,3], 返回 true.示例 2: 1 / \ 2 3二叉树 [1,2,3], 返回 false.我的思路: 中序遍历...
2018-03-28 10:58:04 169
原创 95. Unique Binary Search Trees II
题目描述:给定一个整数 n,生成所有由 1...n 为节点组成的不同的二叉查找树。例如,给定 n = 3,则返回所有 5 种不同形态的二叉查找树: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \...
2018-03-28 10:44:16 215
原创 96. Unique Binary Search Trees
题目描述:给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?例如,给出 n = 3,则有 5 种不同形态的二叉查找树: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ...
2018-03-28 10:14:00 173
原创 94. Binary Tree Inorder Traversal
题目描述:给定一个二叉树,返回其中序遍历。例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [1,3,2].说明: 递归算法很简单,你可以通过迭代算法完成吗?我的思路: 就是非递归遍历二叉树,用栈来实现。基本原则是,如果有左孩子进栈,知道没有左孩子。如果栈不空,弹出栈顶元素,同时去遍历根节点的右子树。在节点不空或者栈不空的情况...
2018-03-28 09:50:01 174
原创 93. Restore IP Addresses(DFS)
题目描述:给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式。例如:给定 "25525511135",返回 ["255.255.11.135", "255.255.111.35"]. (我们可以不考虑数组内元素的顺序)我的思路:我的代码:Discuss:class Solution: def restoreIpAddresses(self, s): """ ...
2018-03-27 19:44:23 365
原创 92. Reverse Linked List II
题目描述:反转从位置 m 到 n 的链表。用一次遍历在原地完成反转。例如:给定 1->2->3->4->5->NULL, m = 2 和 n = 4,返回 1->4->3->2->5->NULL.注意:给定 m,n 满足以下条件:1 ≤ m ≤ n ≤ 列表长度。我的思路: 先设定一个dummy节点标示整个链表,用pre来表...
2018-03-27 19:18:19 175
原创 91. Decode Ways
中文版leetcode上线啦!!!虽然功能尚未完善,但是看题目的时候舒服了不少。题目描述:包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数。例如,给定消息为 "12", 它可以解码为 "AB"(1 2)或 "L"(12)。"12" 的解码方法为 2 种。我的思路: ...
2018-03-27 11:33:56 170
原创 90. 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,...
2018-03-26 19:16:42 200
原创 89. Gray Code
题目描述:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of...
2018-03-26 18:51:15 164
原创 88. Merge Sorted Array
题目描述:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additi...
2018-03-26 17:31:34 136
原创 82. 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-&g...
2018-03-23 11:19:44 150
原创 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.我得思...
2018-03-23 10:11:28 140
原创 81. Search in Rotated Sorted Array II
题目描述:Suppose an array sorted in ascending order 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).Write a function to determine if a given target is i...
2018-03-22 19:54:43 154
原创 80. Remove Duplicates from Sorted Array II
题目描述:Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five elem...
2018-03-22 13:05:08 265
原创 79. 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 verticall...
2018-03-22 12:14:09 387
原创 78. Subsets
题目描述:Given a set of distinct integers, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.For example,If nums = [1,2,3], a solution is:[ [3],...
2018-03-22 11:03:38 174
原创 77. 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],]我的思...
2018-03-21 12:18:34 172
原创 75. 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...
2018-03-21 09:54:19 180
原创 74. 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 ro...
2018-03-20 12:00:57 184
原创 73. 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) spac...
2018-03-20 11:41:21 121
原创 71. Simplify Path
题目描述:Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"click to show corner cases.Corner Cases:Did you consider ...
2018-03-20 10:18:32 142
原创 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?Note: Given n will be a positiv...
2018-03-19 10:41:34 163
原创 69. Sqrt(x)
题目描述:Implement int sqrt(int x).Compute and return the square root of x.x is guaranteed to be a non-negative integer.我的思路: 就是求一个非负数的开方,要求向下取整。 就是在1~x范围中搜索呗,先试了下直接查找1~x/2,超时了。既然是个有序序列就使用二分...
2018-03-19 10:32:42 130
原创 67. Add Binary
题目描述:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".我的思路: 给定连个二进制数,用字符串表示,计算他们的加和,然后也以字符串的形式返回。 先把两个字符串翻转,从低位加到高位,同时用一个p保存进位,如果...
2018-03-19 09:53:27 139
原创 66. Plus One
题目描述:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits ...
2018-03-16 12:27:36 309
原创 63. Unique Paths II
题目描述:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid....
2018-03-16 12:07:37 153
原创 62. Unique Paths
题目描述:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the...
2018-03-15 11:26:50 177
原创 61. Rotate List
题目描述:Given a list, rotate the list to the right by k places, where k is non-negative.Example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.我的思路:给定一个链表(...
2018-03-15 10:54:30 172
原创 60. Permutation Sequence
题目描述:The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""312""321.
2018-03-15 08:44:36 153
原创 59. Spiral Matrix II
题目描述:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6...
2018-03-14 12:10:33 149
原创 58. Length of Last Word
题目描述:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defi...
2018-03-14 11:43:12 149
原创 56. Merge Intervals
题目描述:Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].我的思路: 给出一个类型为intervals的列表,intervals就是一个区间,有start和e...
2018-03-14 10:32:06 134
原创 55. Jump Game
题目描述:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if y...
2018-03-14 08:55:22 211
原创 54. 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 ]]You sho...
2018-03-13 11:06:17 149
原创 53. 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] has...
2018-03-13 08:42:48 151
原创 50. Pow(x, n)
题目描述:Implement pow(x, n).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100我的思路: 题目说明很简单,就是自己实现乘方运算。先试试循环但是估计不行哈。 试了两次水,大致问题是n的类型是int,所以有可能是负数和零,...
2018-03-12 12:05:29 178
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人