
python
bob于
这个作者很懒,什么都没留下…
展开
-
python 的@classmethod &&@staticmethod
class A(object): def foo(self,x): print "executing foo(%s,%s)"%(self,x) @classmethod def class_foo(cls,x): print "executing class_foo(%s,%s)"%(cls,x) @staticmethod转载 2016-05-26 06:25:19 · 369 阅读 · 0 评论 -
DepthBinaryTree leetcode java python
#coding:utf-8class TreeNode: def __init__(self,val,left,right): self.val=val self.left=left self.right=rightclass Solution: def reslove(self,root): ans=[]原创 2016-06-16 18:06:12 · 256 阅读 · 0 评论 -
BuildBinaryTreeby前序遍历和中序遍历 python java leetcode
#coding:utf-8class TreeNode: def __init__(self,val,left,right): self.val=val self.left=left self.right=rightclass Solution: def resolve(self,prestart,instart,isend,pr原创 2016-06-16 18:34:44 · 445 阅读 · 0 评论 -
BinarySortTree java python leetcode
首先我们要了解二叉查找树(二叉排序树)。树中要遵循:树中的左子结点的值要大于右子节点的值。由此我们知道要将数组首先进行排序,然后采用二分法进行构建二叉查找树。#coding:utf-8class TreeNode: def __init__(self,val,left,right): self.val=val self.left=left原创 2016-06-16 20:53:05 · 302 阅读 · 0 评论 -
把玩之python爬虫urllib2高级篇
之前我们设置了一个headers 在构建request时传入。但有些服务器会识别headers中的referer是不是它自己,如果不是,有些服务器是不会响应的。为了对付“反盗链”我们>可以在headers中加入referer,如下: import urllib import urllib2 url="http://..." val转载 2016-06-17 09:40:10 · 609 阅读 · 0 评论 -
把玩之python爬虫cookie篇
原文链接:静觅 » Python爬虫入门六之Cookie的使用为什么要使用cookie? cookie是指网站为了辨别用户身份、进行session跟踪而存储在用户本地终端上的数据(通常是经过加密的) 如果你未登录,想获取某个页面的内容是不被允许的。我们可以利用urllib2保存登录的cookie信息,然后获取页面内容。 1,Opener转载 2016-06-17 15:29:07 · 3541 阅读 · 0 评论 -
把玩之python爬虫urllib2
1,什么是urllib2库? urllib2是python的一个获取URLs的组件,通过urlopen函数的形式来提供了一个非常简单的接口,焗油不同协议获取URLs的能力,urllib2提供了一个比较复杂的接口来处理情况,例如:基础验证,cookies,代理和其他。 我们分析代码: response=urllib2.urlo转载 2016-06-17 08:20:39 · 475 阅读 · 0 评论 -
把玩之python爬虫正则表达式
正则表达式相关注解: 1),数量词的贪婪模式与非贪婪模式 正则表达式通常用于在文本中查找匹配的字符串。python里的数量词默认是贪婪的(在少数语言里可能是默认非贪婪的),总是尝试匹配尽可能多的字符;非贪婪的则相反,总是尝试匹配尽可能少的字符。例如:正则表达式“ab*”如果用于查找“abbbc”,将找到“abbb”。而如果是非贪婪的数量词“ab*?”,将找到“a”。转载 2016-06-17 18:08:42 · 501 阅读 · 0 评论 -
path-sum python java
java版:class TreeNode{ int val; TreeNode left; TreeNode right; void TreeNode(int val){ this.val=val; } }public class Solution{原创 2016-06-17 18:32:25 · 419 阅读 · 0 评论 -
Leetcode path-sum-ii Python Java
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2016-06-19 08:30:25 · 376 阅读 · 0 评论 -
Leetcode FlattenBinaryTree Java Python
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \原创 2016-06-19 08:57:53 · 285 阅读 · 0 评论 -
Distinct Subsequences Python
Given a string S and a string T, count the number of distinct subsequences ofT in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none原创 2016-06-20 07:23:30 · 345 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node Python Java Leetcode
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there原创 2016-06-20 08:06:33 · 317 阅读 · 0 评论 -
把玩之糗事百科简单页面信息爬虫
原文链接:静觅 » Python爬虫实战一之爬取糗事百科段子这个例子是对糗事百科的简单页面爬虫,但是由于糗事百科已经改版,或许运行不成功,主要是为了学习下爬虫完整过程。后序会有改正:请等待。。。#coding:utf-8import urllibimport urllib2import repage=1url='http://www.qiushibaike.com/hot/转载 2016-06-20 16:19:59 · 461 阅读 · 0 评论 -
Pascal's Triangle Leetcode Python Java
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]Python:#coding:utf-8class原创 2016-06-20 16:34:56 · 302 阅读 · 0 评论 -
leetcode BinaryTreeLevel java python
java版本:class TreeNode{ int val; TreeNode left=null; TreeNode right=Null; void TreeNode(val){ this.val=val; }}public class BinaryTreeNode{ public List> binarytree(Tre原创 2016-06-16 17:26:30 · 277 阅读 · 0 评论 -
3Sum Closest Leetcode Python Java
16. 3Sum ClosestTotal Accepted: 82615 Total Submissions: 279812 Difficulty: MediumGiven an array S of n integers, find three integers in S such that the sum is closest to a given n原创 2016-06-28 11:52:05 · 396 阅读 · 0 评论 -
python 的类变量和实例变量
如名所示,类变量是类实用的变量,实例变量引用的类的变量,且实例变量的作用域会影响类变量的引用。class Person: name=[] age=1 p1=Person()p2=Person()p1.name.append('bob')p1.age=2print p1.name #['bob']print p1.age#2print p2.name转载 2016-05-26 07:02:09 · 310 阅读 · 0 评论 -
python的彪悍特性--自省
自省其实就是在运行时能够得到的对象的类型。如:type(),dir(),getattr(),hasstty(),isinstance().转载 2016-05-26 07:07:07 · 337 阅读 · 0 评论 -
python 的单下划线和双下划线
class MyClass(): def __init__(self): self.__superprivate = "Hello" self._semiprivate = ", world!"mc=MyClass()print mc.__superivate#错误print mc._semiprivate#,worldp转载 2016-05-26 07:16:59 · 417 阅读 · 0 评论 -
python的%sVS.format
To answer your first question... .format just seems more sophisticated in many ways. An annoying thing about% is also how it can either take a variable or a tuple. You'd think the following would al转载 2016-05-26 07:22:37 · 1536 阅读 · 0 评论 -
python的关键字yield有什么作用?
要理解yiled还需要理解生成器,而要理解生成器,首先需要理解迭代器。迭代器:所有你可以用在for...in...语句中的都是可迭代的:比如lists,strings,files...因为这些可迭代的对象你可以随意的读取所以非常方便易用,但是你必须把它们的值放到内存里,当它们有很多值时就会消耗太多的内存.生成器:生成器也是迭代器的一种,但是你只能迭代它们一次.原因很简单,因转载 2016-05-26 07:33:38 · 5103 阅读 · 0 评论 -
python的args和kwargs
当函数的参数不确定时,可以使用argrs和kwargs。*args没有key值,而**kwargs是有key值的。*args可以表示为任意长度的tuple,可以接受一连串的参数。**kwargs表示一个dictionary,参数的形式是“key=value”。当两者同时使用时,args在前kwargs在后。转载 2016-05-26 07:54:42 · 415 阅读 · 0 评论 -
Two Sum Leetcode Python Java
Two SumGiven an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:Giv原创 2016-06-27 16:23:52 · 339 阅读 · 0 评论 -
Add Two Sum Leetcode Python java
Add Two NumbersTotal Accepted: 153367 Total Submissions: 644131 Difficulty: MediumYou are given two linked lists representing two non-negative numbers. The digits are stored原创 2016-06-27 16:58:56 · 298 阅读 · 0 评论 -
Longest Substring Without Repeating Characters Leetcode Python Java
Longest Substring Without Repeating CharactersTotal Accepted: 159876 Total Submissions: 710693 Difficulty: MediumGiven a string, find the length of the longest substring with原创 2016-06-27 17:21:47 · 263 阅读 · 0 评论 -
Longest Palindromic Substring Leetcode Python Java
Longest Palindromic SubstringTotal Accepted: 115663 Total Submissions: 493295 Difficulty: MediumGiven a string S, find the longest palindromic substring in S. You may assume原创 2016-06-27 18:36:19 · 502 阅读 · 0 评论 -
Reverse Integer Leetcode Python Java
Reverse IntegerTotal Accepted: 147726 Total Submissions: 622694 Difficulty: EasyReverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321原创 2016-06-28 08:08:47 · 257 阅读 · 0 评论 -
Palindrome Number Leetcode Python Java
Palindrome NumberTotal Accepted: 131143 Total Submissions: 406290 Difficulty: EasyDetermine whether an integer is a palindrome. Do this without extra space.Java:public原创 2016-06-28 08:22:49 · 289 阅读 · 0 评论 -
Longest Common Prefix Leetcode Python Java
Longest Common PrefixTotal Accepted: 107085 Total Submissions: 371911 Difficulty: EasyWrite a function to find the longest common prefix string amongst an array of strings.原创 2016-06-28 09:48:27 · 290 阅读 · 0 评论 -
3Sum Leetcode Python Java
3SumTotal Accepted: 125606 Total Submissions: 653184 Difficulty: MediumGiven an array S of n integers, are there elements a,b, c in S such that a + b + c = 0? Find all uniq原创 2016-06-28 11:15:55 · 340 阅读 · 0 评论 -
Pascal's Triangle II Leetcode Python java
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Python:#coding:utf-8class Solution: def PascalTriangleii(self,k):原创 2016-06-20 16:39:27 · 267 阅读 · 0 评论