自定义博客皮肤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的小屋

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

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

原创 [leetcode]Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), d...

2018-05-01 21:24:56 195

原创 【leetcode】Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7],...

2018-05-01 21:09:33 258

原创 [leetcode]Pascal's Triangle II

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.Note that the row index starts from 0.In Pascal's triangle, each number is the sum of the two numbers direc...

2018-05-01 20:41:06 281

原创 [Leetcode]Pascal's Triangle

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.In Pascal's triangle, each number is the sum of the two numbers directly above it.Example:Input: 5Output:[ ...

2018-05-01 14:38:13 220

原创 【leetcode】 Populating Next Right Pointers in Each Node

Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node...

2018-05-01 14:24:47 138

原创 [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 121

原创 [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 191

原创 [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 119

原创 [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 82

原创 [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 96

原创 [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 94

原创 [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 151

原创 【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 128

原创 [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 95

原创 [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 95

原创 【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 161

原创 [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 125

原创 [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 106

原创 [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 164

原创 [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 137

原创 [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 95

原创 [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 120

原创 [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 464

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

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

2018-04-21 20:45:35 188

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

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

2018-04-21 20:29:29 947

原创 【剑指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 118 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 114

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

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

2018-04-16 12:57:36 103

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

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

2018-04-16 12:57:31 103

原创 【剑指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 94

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

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

2018-04-16 12:57:18 152

原创 【剑指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 111

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

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

2018-04-16 12:57:09 67

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

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

2018-04-16 12:57:04 94

原创 【剑指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 153

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

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

2018-04-16 12:56:53 104

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

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

2018-04-15 15:34:21 83

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

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

2018-04-15 15:00:12 73

原创 【剑指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 75

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

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

2018-04-15 14:33:00 99

空空如也

空空如也

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

TA关注的人

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