自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

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

原创 Summary Ranges

地址:点击打开链接这个看是不是连续的数,如果是话就给出开始和结尾的数,如果不是的话,就重新开始,设定begin和end指针遍历就行class Solution(object): def summaryRanges(self, nums): """ :type nums: List[int] :rtype: List[str]

2015-10-27 16:49:54 237

原创 Binary Tree Paths

地址:点击打开链接得到路径的时候判断一下叶节点,然后对是不是根节点判断一下,再用递归的方法答案:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None

2015-10-27 16:11:00 177

原创 Implement strStr()

地址:点击打开链接这道题就是求一个字符串是否是另一个字符串的子字符串,所以第一种方法首先想到的是KMP算法,其次用暴力破解,还有用素数代表字符,然后根据进位来计算相关和的方法,但是容易发生溢出,所以就需要采用,大数值或者用KMP算法,这里python的数值好像溢出了,所以就采用KMP,看了两天,里面难点是next数组的求法,具体求法参见july的博客答案:class Sol

2015-10-27 10:43:10 172

原创 Palindrome Linked List

地址:点击打开链接遍历一遍链表存到数组里面,然后将数组进行头尾遍历,看是否相等答案:# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Sol

2015-10-21 16:49:39 159

原创 Add Binary

地址:二进制计算,主要注意标志位的判断答案:class Solution(object): def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ ret = "" flag =

2015-10-21 16:40:17 157

原创 Count and Say

地址:点击打开链接看不懂题目,后来百度了下才知道,当前数字需要对前面一个数字进行计算,相邻而且相同的数的个数即为下一个字符串比如第一个数是1,算第二个数时,前一个数有一个1所以当前字符串为11,下个数就是2个1即为21读懂题目就很好算了,计数即可答案:class Solution(object): def countAndSay(self, n):

2015-10-21 16:10:04 147

原创 Isomorphic Strings

地址:点击打开链接和Word Pattern方法相似,答案都是一种答案:class Solution(object): def isIsomorphic(self, s, t): """ :type s: str :type t: str :rtype: bool """ r

2015-10-21 14:44:44 148

原创 Word Pattern

地址:点击打开链接单词对应,这个有两种方法都是用了python的内置函数,很简单方便class Solution(object): def wordPattern(self, pattern, str): """ :type pattern: str :type str: str :rtype: bool

2015-10-21 14:40:09 149

原创 Remove Linked List Elements

地址:删除链表中为给定的值的元素的节点注意判断条件答案:# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object

2015-10-20 13:48:46 125

原创 Rectangle Area

地址:点击打开链接题目要求技术两个矩形覆盖面积,如果相交则减去两个矩形面积相交的部分,如果不相交则仅算两个矩形的面积答案:class Solution(object): def computeArea(self, A, B, C, D, E, F, G, H): """ :type A: int :type B: int

2015-10-20 11:50:36 146

原创 Valid Parentheses

地址:点击打开链接括号匹配,用栈即可答案:class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ if s == '': return False stac

2015-10-20 11:32:15 95

原创 Contains Duplicate II

地址:点击打开链接题目大意:给定一个整数数组nums与一个整数k,当且仅当存在两个不同的下标i和j满足nums[i] = nums[j]并且| i - j | 所以用遍历的方法是不行的,设定滑动窗口也是有点问题,所以在python中可以用字典的方法,感觉这个比较容易理解答案:if nums == []: return False numdic

2015-10-20 11:13:51 128

原创 Merge Sorted Array

地址:点击打开链接算法要求合并两个有序数组,其中将数组二的元素合并到数组1中,数据结构书中合并有序数组是新建一个数组,然后依次比较大小,这个题如果按照这种思路,明显时间复杂度是o(n*n),所以有一个简单的方法,从尾部开始比较,因为数组1的长度足够,所以设计从两个数相加的长度开始比较即可不知道什么原因python的代码一直通不过,我写了个java的反倒过了答案:class

2015-10-19 13:53:11 131

原创 Palindrome Number

地址:点击打开链接题目要求判断是否回文数字,而且要求不能用额外空间,意味着将数字转字符串不行,将数字颠倒也不行,那只有判断高位和地位数字是否相同了,而且判断条件要注意答案:class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype:

2015-10-19 11:00:05 126

原创 Minimum Depth of Binary Tree

地址:点击打开链接其实就是树遍历的变种问题,可以用深度和层次遍历,我选择了深度遍历,注意对根节点的判断,理由是如果根节点如果左右子树只有一颗不为空,则这个子树即为最短路径的一部分答案:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):#

2015-10-19 09:45:20 138

原创 Factorial Trailing Zeroes

地址:点击打开链接n!后缀0的个数 = n!质因子中5的个数 = floor(n/5) + floor(n/25) + floor(n/125) + ....根据公式解出来class Solution(object): def trailingZeroes(self, n): """ :type n: int

2015-10-16 11:56:19 133

原创 Intersection of Two Linked Lists

地址:点击打开链接求两个链表的交集节点,用双指针来遍历ps:这道题是微软的一道题,当年微软也考过,时光飞逝啊....答案:# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x#

2015-10-16 10:55:25 121

原创 Pascal's Triangle II

地址:点击打开链接这个三角的变形版本,下标有点变化class Solution(object): def getRow(self, rowIndex): """ :type rowIndex: int :rtype: List[int] """ return self.generate(rowI

2015-10-16 10:08:32 159

原创 Path Sum

地址:点击打开链接这道题是实际是遍历树然后求和,只不过加了一个参数,所以要会写BFS和DFS,以及层次遍历# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = No

2015-10-15 15:03:08 148

原创 Pascal's Triangle

地址:点击打开链接题目比较简单,很容易算答案:class Solution(object): def generate(self, numRows): """ :type numRows: int :rtype: List[List[int]] """ if numRows == 0:

2015-10-15 09:34:49 136

原创 House Robber

地址:点击打开链接题目大意,一个非负数组,求不相邻元素的最大和,这类问题可以统统归结为动态规划问题,将总问题拆分为子问题,核心公式为d(i) = max(d(i-1), d(i-1)+nums[i])答案:class Solution(object): def rob(self, nums): """ :type nums: List[int]

2015-10-14 16:30:29 147

原创 Remove Duplicates from Sorted Array

地址:点击打开链接要求删除数组中重复的元素,同时保留出现一次的元素用个中间变量保存即可答案:class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if

2015-10-13 13:33:35 120

原创 Power of Two

地址:这里判断是否为2的幂函数,想到最传统的方法来判断,逐层计算,得到结果(实际上有更好的方法,只是我暂时还想不到)答案:class Solution(object): def isPowerOfTwo(self, n): """ :type n: int :rtype: bool """

2015-10-13 09:54:35 106

原创 Remove Element

地址:点击打开链接删除数组中所有和输入值相同的元素,遍历删除的时间复杂度很高,移动元素需要耗费大量时间,所以想到定义一个头结点和尾节点,然后将相同元素移到一起,最后一起删除即可,有问题的地方在于最后endIndex需要判断一下答案:class Solution(object): def removeElement(self, nums, val): """

2015-10-13 09:36:38 129

原创 Merge Two Sorted Lists

地址点击打开链接很简单,合并两个单链表,书上的内容答案:# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object

2015-10-10 13:32:47 132

原创 Implement Queue using Stacks

地址:点击打开链接题目是让用栈来实现队列,这里就利用栈的先入后出的特性,用两个栈来实现队列,需要注意的是队列的操作的判断条件答案:class Queue(object): def __init__(self): """ initialize your data structure here. """ sel

2015-10-09 15:54:03 129

原创 Reverse Linked List

地址:点击打开链接要求翻转单链表,我想的是新建一个栈,然后遍历两边链表,第一遍值入栈,第二遍逐个出栈赋值即可答案:# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next =

2015-10-09 14:10:46 112

原创 Climbing Stairs

地址:我的第一思路是用排序的方法做,但是考虑了很多种情况# ret = 0 # if n == 0: # return 0 # for m in range(0,n+1): # if (n-m) % 2 == 0: # temp = (n-m) / 2 #

2015-10-09 10:46:29 120

原创 Remove Duplicates from Sorted List

地址:点击打开链接题目的要求是删除链表中重复的元素,所以用两个指针指向即可,注意下判断条件答案:# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Nonecla

2015-10-08 14:21:44 107

原创 Excel Sheet Column Number

题目地址:这里我的答案class Solution(object): def titleToNumber(self, s): """ :type s: str :rtype: int """ s = s.upper() total = 0 if s == '':

2015-09-30 12:25:03 129

原创 Number of 1 Bits

题目:这里我的答案class Solution(object): def hammingWeight(self, n): """ :type n: int :rtype: int """ num = 0 while n > 0: if n%2 != 0:

2015-09-30 10:42:21 106

原创 Invert Binary Tree

有大神也不能写出来的翻转二叉树题目地址:这里关键点在于将根的左右子树翻转,然后将子树的左右子树再翻转,加上层次遍历,用队列保存遍历结果我的答案# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x#

2015-09-30 10:31:17 160

原创 Lowest Common Ancestor of a Binary Search Tree

题目:这里我的答案:# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solut

2015-09-30 10:26:18 119

原创 Move Zeroes

题目见这里我的答案class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """

2015-09-28 14:55:28 132

原创 Delete Node in a Linked List

题目见这里我的答案class Solution(object): def deleteNode(self, node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """

2015-09-28 10:47:46 136

原创 Maximum Depth of Binary Tree

具体问题见这里代码class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = Noneclass Solution(object): def maxDepth(self, root): "

2015-09-28 09:52:55 117

原创 chapter4 python技巧

1.深拷贝和浅拷贝1.1 copy模块FUNCTIONS    copy(x)        Shallow copy operation on arbitrary Python objects.                See the module's __doc__ string for more info.        deepco

2015-09-11 11:24:42 72

原创 1.9 简化字符串的translate方法的使用

对字符串translate的封装预备知识函数闭包def line_conf(): def line(x): return 2*x+1 return line #返回的是一个函数对象t = line_conf()print t(4)#结果是9函数闭包常用于装饰器,作用是为了避免频繁修改函数封装def translator(frm

2015-09-01 13:31:46 165

原创 第三章 决策树(一)

1.预备知识1.信息增益在信息论与概率统计中,熵(entropy)是表示随机变量的不确定性的度量,熵值越大表明越不确定,设X是随机变量,则熵定义如下     计算信息增益如下:2代码解析#coding:gbk'''Created on 2014年11月4日@author: dell'''from numpy import log2import oper

2015-04-29 22:23:38 205

原创 KNN(三) 用Matplolib分析数据

1预备知识安装Matplotlib库的过程不提,百度后都一一解决2资料http://matplotlib.org/api/pyplot_summary.htmlhttp://wenku.baidu.com/view/dcee911cfad6195f312ba61c.htmlhttp://liam0205.me/2014/09/11/matplotlib-tuto

2015-04-29 20:17:00 403

空空如也

空空如也

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

TA关注的人

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