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 constant extr原创 2014-10-05 22:12:22 · 701 阅读 · 0 评论 -
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},原创 2014-10-05 21:18:11 · 921 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array
Find Minimum in Rotated Sorted Array原创 2014-12-01 21:04:53 · 708 阅读 · 0 评论 -
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?思路:爬楼梯的问题,斐波那契数列,应该见过很多回了原创 2014-12-01 23:48:41 · 603 阅读 · 0 评论 -
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.思路:简单的大数加法class原创 2014-12-01 23:35:01 · 542 阅读 · 0 评论 -
【leetCode】 Balanced Binary Tree python版实现
原题链接Balanced Binary Tree实现原理解析 该题比较简单,主要思想是递归的判断左右子树的高度不大于1即可,注意异常处理python代码实现class Solution(object): def isBalanced(self, root): """ :type root: TreeNode :rtype: bool原创 2016-06-18 16:56:00 · 1395 阅读 · 0 评论 -
【contains-duplicate】leetCode python实现
原题链接contains-duplicate实现原理解析 该题比较简单,比较去重后的长度和原始长度是否一致即可python代码实现class Solution(object): def containsDuplicate(self, nums): """ :type nums: List[int] :rtype: bool原创 2016-06-18 17:16:09 · 636 阅读 · 0 评论 -
【leetCode】Path Sum II python实现
Path Sum II原题链接Path Sum II实现原理解析 该题使用深度搜索实现即可python代码实现import copy# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.le原创 2016-06-18 21:54:06 · 1618 阅读 · 0 评论 -
【leetCode】Sum Root to Leaf Number python实现
Sum Root to Leaf Numbers原题链接Path Sum II实现原理解析 该题使用深度搜索找到所有路径,然后把每个路径下的数加起来即可python代码实现import copyclass Solution(object): def __init__(self): self.paths = [] self.path = []原创 2016-06-18 22:40:32 · 543 阅读 · 0 评论 -
【leetCode】Binary Tree Zigzag Level Order Traversal python实现
Binary Tree Zigzag Level Order Traversal原题链接Binary Tree Zigzag Level Order Traversal实现原理解析 层次遍历即可,对于偶数层的做反向python代码实现import copy# Definition for a binary tree node.# class TreeNode(object):# d原创 2016-06-19 01:18:58 · 1293 阅读 · 0 评论 -
【leetCode】Binary Tree Level Order Traversal python实现
Binary Tree Level Order Traversal原题链接Binary Tree Level Order Traversal实现原理解析 层次遍历即可python代码实现class Solution(object): def __init__(self): self.result = [] def levelOrder(self, root):原创 2016-06-19 01:41:14 · 1051 阅读 · 0 评论 -
【leetCode】House Robber python实现
House Robber原题链接House Robber实现原理解析 动态规划实现python代码实现class Solution(object): def rob(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums) ==原创 2016-06-19 02:20:41 · 934 阅读 · 0 评论 -
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.点击打开原题链接这个题剑指offer里也有,简单的递归即可,代码很清晰:原创 2014-10-05 20:30:44 · 793 阅读 · 0 评论 -
4Sum_leetCode
Given an array S of n integers, are there elements a,b, c, and d in S such that a + b +c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements原创 2014-10-05 16:34:48 · 768 阅读 · 0 评论 -
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原创 2014-10-05 23:17:58 · 794 阅读 · 0 评论 -
Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.原题链接原创 2014-09-29 18:43:04 · 563 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3]原创 2014-09-29 19:11:47 · 477 阅读 · 0 评论 -
Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1", "+",原创 2014-09-29 16:48:57 · 468 阅读 · 0 评论 -
Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->原创 2014-09-29 20:10:54 · 749 阅读 · 0 评论 -
Reverse Words in a String
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".Clarification:What constitutes a word?A sequence of non-space ch原创 2014-09-29 16:15:36 · 639 阅读 · 0 评论 -
Subsets
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,I原创 2014-10-03 19:56:11 · 914 阅读 · 0 评论 -
Decode Ways
//下述算法应该是对的,不过超时// class Solution // {// public:// int numDecodings(string s) // {// if (s.length() == 0)// {// return 0;// }// else if (s.length() == 1)// {// if (s ==原创 2014-10-03 12:53:14 · 730 阅读 · 0 评论 -
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 bo原创 2014-10-03 14:16:28 · 631 阅读 · 0 评论 -
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 /原创 2014-10-03 10:52:08 · 530 阅读 · 0 评论 -
Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m +n) to hold additional elements from B原创 2014-10-05 16:16:25 · 474 阅读 · 0 评论 -
【leetCode】House Robber II python实现
原题链接House Robber II实现原理解析 该题在House Robber的基础上让首位链接形成环,那么即表示第一个和最后一个不能同时被抢,则问题分解为House Robber(nums[0:len(nums)-1])和House Robber(nums[1:len(nums)]),两者中比较大的那个即为结果python代码实现class Solution(object): d原创 2016-06-19 21:25:46 · 1313 阅读 · 0 评论