自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

benjamin.li的小屋

认真投入的做一件事情,让这件事情有结果

  • 博客(61)
  • 收藏
  • 关注

原创 [Leetcode]Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.For example, given the following tree: 1 / \ 2 5 / \ \3 4 6The flattened tree should look like:1 \ 2 \ 3 \ ...

2018-04-29 11:31:21 150

原创 [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.Note: A leaf is a node with no children.Example:Given the below binary tree and sum = 22, 5 ...

2018-04-27 11:48:22 246

原创 [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.Note: A leaf is a node with no children.Example:Give...

2018-04-26 16:38:38 128

原创 [Leetcode]Minimum Depth of Binary Tree

给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回它的最小深度  2.思路:这个题跟最大深度类似,不过如果有一侧没树枝,就不算。。。是个坑# Definition for a ...

2018-04-26 16:21:41 92

原创 [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 de...

2018-04-26 16:00:52 127

原创 [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 t...

2018-04-26 15:37:40 124

原创 [Leetcode]Jump Game II

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.Your goal is to rea...

2018-04-23 20:09:56 162

原创 【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.The above elevation map is represented by array [0...

2018-04-23 14:55:17 138

原创 [leetcode]First Missing Positive

Given an unsorted integer array, find the smallest missing positive integer.Example 1:Input: [1,2,0]Output: 3Example 2:Input: [3,4,-1,1]Output: 2Example 3:Input: [7,8,9,11,12]Output: 1要求O(n)的时间,...

2018-04-23 14:34:26 120

原创 [leetcode]Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.Example 1:Input: "(()"Output: 2Explanation: The longest valid pare...

2018-04-23 14:10:28 107

原创 【leetcode】Next Permutation(全排列)

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible ord...

2018-04-23 11:36:26 189

原创 [Leetcode]Substring with Concatenation of All Words

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and wi...

2018-04-23 11:19:03 155

原创 [leetcode]Divide Two Integers

Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll"Output: 2Example 2:Input:...

2018-04-22 17:26:37 133

原创 [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.k is a positive integer and is less than or equal to the length of the linked list. If the number of no...

2018-04-22 14:49:00 191

原创 [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). Find two ...

2018-04-21 22:24:20 167

原创 [leetcode]Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.Example:Given 1->2->3->4, you should return the list as 2->1->4->3.Note:Your algorithm should use only constant...

2018-04-21 22:10:34 111

原创 [leetcode]Merge k Sorted Lists@python

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.Example:Input:[  1->4->5,  1->3->4,  2->6]Output: 1->1->2->3->4->4...

2018-04-21 21:43:59 143

原创 [Leetcode]Regular Expression Matching@python

Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The...

2018-04-21 21:17:25 479

原创 吴恩达《Machine Learning Yearning》学习笔记(二):模型指标

1.建立单一数字的评估指标        指标太多会影响不同模型的判别,找一个你最关心的性能指标,以它为基准,再进行模型的调试,会起到事半功倍的效果2.优化指标和满足指标        这是组合多个评估指标的另一种方法。假设你同时关心算法的准确率和运行时间。你需要在下面三个分类器中进行选择:  这里如果将准确率和运行时间组合为单个评估指标会看起来不太自然,例如:Accuracy−0.5∗Runni...

2018-04-21 20:45:35 251

原创 吴恩达《Machine Learning Yearning》学习笔记(一):开放集与测试集

1.你的开放集与测试集(1)训练集:不言而喻(2)开放集:在训练时用来测试模型并调整模型参数的数据(3)测试集:模型训练好后用来评估模型的数据        一般而言,训练集和测试集三七分。但你要特别注意的开放集和测试集。因为,开放集和测试集代表着你希望模型朝着某个最重要的方向进行训练。        因此,选择的开放集和测试集要能够反映你未来得到的数据。所以,30%的测试集不要随便选。比如做一个...

2018-04-21 20:29:29 970

原创 【剑指offer】滑动窗口的最大值

题目描述给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,...

2018-04-18 14:02:50 127 2

原创 【剑指offer】把二叉树打印成多行

题目描述从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。思路:依然是层序遍历。。# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclas...

2018-04-16 12:57:50 123

原创 【剑指offer】把字符串转换成整数

题目描述将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0思路:本题考查对问题理解的全面性。要考虑负数情况,考虑非法字符串情况。# -*- coding:utf-8 -*-class Solution: def StrToInt(self, s): # write code here if not...

2018-04-16 12:57:36 112

原创 【剑指offer】数组中重复的数字

题目描述在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。思路:hash思想,不用担心不能跳出循环。因为题目说了,如果没有重复,数字一定为0~n-1的每个数都有/*从头扫到尾,只要当前...

2018-04-16 12:57:31 128

原创 【剑指offer】构建乘积数组

题目描述给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。//B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]//从左到右算 B[i]=A[0]*A[1]*...*A[i-1]//从右到左算B[i]*=A[i+...

2018-04-16 12:57:26 103

原创 【剑指offer】字符流中第一个不重复的字符

题目描述请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。思路:dict统计次数# -*- coding:utf-8 -*-class Solution: # 返回对应char def __init__(self): ...

2018-04-16 12:57:18 165

原创 【剑指offer】链表中环的入口

题目描述一个链表中包含环,请找出该链表的环的入口结点。 假设x为环前面的路程(黑色路程),a为环入口到相遇点的路程(蓝色路程,假设顺时针走), c为环的长度(蓝色+橙色路程) 当快慢指针相遇的时候: 此时慢指针走的路程为Sslow = x + m * c + a 快指针走的路程为Sfast = x + n * c + a 2 Sslow = Sfast 2 * ( x + m*c +...

2018-04-16 12:57:14 121

原创 【剑指offer】二叉树的下一个结点

题目描述给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。思路:分析二叉树的下一个节点,一共有以下情况: 1.二叉树为空,则返回空; 2.节点右孩子存在,则设置一个指针从该节点的右孩子出发,一直沿着指向左子结点的指针找到的叶子节点即为下一个节点; 3.节点不是根节点。如果该节点是其父节点的左孩子,则返回...

2018-04-16 12:57:09 74

原创 【剑指offer】之字形打印二叉树

题目描述请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。思路:层序遍历,维护一个flag即可# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# sel...

2018-04-16 12:57:04 103

原创 【剑指offer】把二叉树打印成多行

题目描述从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。思路:依然是层序遍历。。# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclas...

2018-04-16 12:56:59 165

原创 【剑指offer】二叉树的下一个结点

题目描述给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。思路:变相考查中序遍历# -*- coding:utf-8 -*-# class TreeLinkNode:# def __init__(self, x):# self.val = x# self.left =...

2018-04-16 12:56:53 114

原创 【剑指offer】不用加减乘除做加法

题目描述写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。思路://step1:按位与是查看两个数哪些二进制位都为1,这些都是进位位,结果需左移一位,表示进位后的结果//step2:异或是查看两个数哪些二进制位只有一个为1,这些是非进位位,可以直接加、减,结果表示非进位位进行加操作后的结果//step3:n1&n2是查看有没有进位位了,如果有,需要重复step1...

2018-04-15 15:34:21 93

原创 【剑指offer】圆圈中最后剩下的数

题目描述每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去.......

2018-04-15 15:00:12 86

原创 【剑指offer】扑克牌顺子

题目描述LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为1...

2018-04-15 14:37:17 81

原创 【剑指offer】左旋转字符串

题目描述汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!思路:先整体翻转,再局部翻转# -*- coding:utf-8 -*-class Solution...

2018-04-15 14:33:00 113

原创 【剑指offer】和为S的两个数字

题目描述输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。数列满足递增,设两个头尾两个指针i和j, 若ai + aj == sum,就是答案(相差越远乘积越小) 若ai + aj > sum,aj肯定不是答案之一(前面已得出 i 前面的数已是不可能),j -= 1 若ai + aj < sum,a...

2018-04-15 14:26:47 80

原创 【剑指offer】和为S的连续正数序列

题目描述小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!思路:/**初始化small=1,big=2;*...

2018-04-15 14:19:46 76

原创 【剑指offer】两个链表的第一个公共结点

题目描述输入两个链表,找出它们的第一个公共结点。思路:先统计两个链表长度,长的链表先走长的部分,然后一起走,判断是否相等# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: ...

2018-04-15 13:35:58 81

原创 【剑指offer】数字在排序数组中出现的次数

题目描述统计一个数字在排序数组中出现的次数。思路:这个题最优解法是直接用两个二分法找左右两边的index,所以,这里我们直接找k-0.5, k+0.5的index就可以解决问题。但是这里要特别注意边界条件,是while left <= right# -*- coding:utf-8 -*-class Solution: def GetNumberOfK(self, data, k)...

2018-04-15 13:35:51 97

原创 【剑指offer】判断是否是平衡二叉树

题目描述输入一棵二叉树,判断该二叉树是否是平衡二叉树。思路:判断树的左右分支最大深度差是否大于1,然后递归# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = None...

2018-04-15 13:35:44 93

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除