12 2016 档案

摘要:初始化超类的传统方式,在子类的实例中调用超类的__init__()方法。 但是传统的方法有两个问题,比如: 问题1: 结果为: 从结果可以看出,即使改变了子类的继承顺序,调用的顺序并没有改变。 问题2: 如果子类继承自两个单独的超类,而那两个超类又继承自同一个公共基类,那么就构成了钻石型继承。 这种 阅读全文
posted @ 2016-12-31 23:15 wilderness 阅读(3141) 评论(0) 推荐(0)
摘要:使用生成器的好处是显而易见的,可以使代码更加清晰,同时减小内存的消耗,当函数需要返回列表,把函数改写为生成器是相对容易的。 下面这两个函数返回字符串中每个单词的索引: 参考资料:Effective Python 阅读全文
posted @ 2016-12-31 22:26 wilderness 阅读(252) 评论(0) 推荐(0)
摘要:如果要达到多个迭代器的效果,__iter__()只需替迭代器定义新的状态对象,而不是返回self 参考资料:Python学习手册 阅读全文
posted @ 2016-12-31 22:05 wilderness 阅读(657) 评论(0) 推荐(0)
摘要:对迭代器和生成器的概念一直很混乱,总结一下: 迭代器: 1.所谓的迭代器,就是具有__next__()方法的对象; 2.__iter__()方法返回一个迭代器对象,这个对象必须具有__next__()方法; 3.一个实现了__iter__()方法的对象是可迭代的,一个实现了__next__()方法的 阅读全文
posted @ 2016-12-31 21:48 wilderness 阅读(198) 评论(0) 推荐(0)
摘要:第一次见到functools.wraps是在 Flask Web开发 中,一直不明白怎么回事。 装饰器(decorator)是干嘛的?对于受到封装的原函数来说,装饰器能够在那个函数执行前或者执行后分别运行一些代码,使得可以再装饰器里面访问并修改原函数的参数以及返回值,以实现约束定义、调试程序、注册函 阅读全文
posted @ 2016-12-31 18:16 wilderness 阅读(14684) 评论(1) 推荐(0)
摘要:首先判断一个字符串是否回文: 阅读全文
posted @ 2016-12-27 12:32 wilderness 阅读(639) 评论(0) 推荐(0)
摘要:Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 题意:返回在字符串中子字符串出现的第一个位置,没 阅读全文
posted @ 2016-12-24 13:39 wilderness 阅读(142) 评论(0) 推荐(0)
摘要:Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you 阅读全文
posted @ 2016-12-24 13:24 wilderness 阅读(169) 评论(0) 推荐(0)
摘要:Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space fo 阅读全文
posted @ 2016-12-24 12:53 wilderness 阅读(193) 评论(0) 推荐(0)
摘要:Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your a 阅读全文
posted @ 2016-12-24 12:29 wilderness 阅读(118) 评论(0) 推荐(0)
摘要:八皇后问题: 把N个皇后,放在N*N的棋盘上面,从第一行往下放,每个皇后占一行,同时,每个皇后不能处在同一列,对角线上,有多少种放置方法。 思路: 典型的回溯问题: 1.当要放置最后一个皇后时候,默认前N-1个皇后已经全部放置好了,那么验证在第N行上的每个位置是否可行,即是否与之前的皇后在同一列或者 阅读全文
posted @ 2016-12-23 23:46 wilderness 阅读(400) 评论(0) 推荐(0)
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题意:合并两个已 阅读全文
posted @ 2016-12-23 18:39 wilderness 阅读(194) 评论(0) 推荐(0)
摘要:Given a linked list, remove the nth node from the end of list and return its head. For example, 题意:给出一个链表,删除倒数第n个节点 刚在书上见过这个题,思路是用双指针,p1,p2,先让p2走n步,然后 阅读全文
posted @ 2016-12-23 17:56 wilderness 阅读(160) 评论(0) 推荐(0)
摘要:l=[1, 2, 3, 4, 5, 6] 如果l求和,毫无疑问可以使用递归,比如可以这样: 如果我们想把l中的所有子列表裂解开,这种方法行不行。。。?比如可以这样 方法1: 如果使用生成器的话: 方法2 可以看到方法2看起来简单了很多,但是方2都有一个问题,如果有一个列表是这样的 那么方法2还可行吗 阅读全文
posted @ 2016-12-23 15:23 wilderness 阅读(1011) 评论(0) 推荐(0)
摘要:Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telepho 阅读全文
posted @ 2016-12-22 23:59 wilderness 阅读(167) 评论(0) 推荐(0)
摘要:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. 阅读全文
posted @ 2016-12-22 23:04 wilderness 阅读(131) 评论(0) 推荐(0)
摘要:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of 阅读全文
posted @ 2016-12-22 17:00 wilderness 阅读(172) 评论(0) 推荐(0)
摘要:Write a function to find the longest common prefix string amongst an array of strings. 题意:找出所给几个字符串的相同前缀 思路:用第一个字符串和之后的所有字符串进行对比,标示出相同字符串的超尾指针就行 ps:通过 阅读全文
posted @ 2016-12-22 13:28 wilderness 阅读(208) 评论(0) 推荐(0)
摘要:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题意:把罗马数字转变为数字 阅读全文
posted @ 2016-12-22 12:14 wilderness 阅读(139) 评论(0) 推荐(0)
摘要:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题意:把数字转换为罗马数字 感觉用c的话太麻烦了,所以用Python写了 阅读全文
posted @ 2016-12-22 00:01 wilderness 阅读(180) 评论(0) 推荐(0)
摘要:python通过queue模块来提供线程间的通信机制,从而可以让线程分项数据。 个人感觉queue就是管程的概念 一个生产者消费者问题 输出结果: 阅读全文
posted @ 2016-12-21 23:08 wilderness 阅读(414) 评论(0) 推荐(0)
摘要: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 endpo 阅读全文
posted @ 2016-12-21 18:43 wilderness 阅读(178) 评论(0) 推荐(0)
摘要:信号量适用与多线程竞争有限资源的情况。 输出结果: 参考资料:Python核心编程.第四章.Wesley Chun著 阅读全文
posted @ 2016-12-20 23:46 wilderness 阅读(366) 评论(0) 推荐(0)
摘要:当出现竞态条件时候,即在同一个时刻只有一个线程可以进入临界区,需要使用同步。 常见的同步原语有两种:锁/互斥,信号量。 锁是最简单,最低级的机制。 首先看一个不使用锁时候的多线程示例: 输出结果1: Thread-1 starting at Tue Dec 20 23:12:03 2016Threa 阅读全文
posted @ 2016-12-20 23:26 wilderness 阅读(406) 评论(0) 推荐(0)
摘要:theading模块的Thread类 属性: name 线程名 ident 线程标识符 daemon 布尔值,标示是否为守护线程 方法: __init__(target=None, name=None, *args=(), **kwargs={}) start() 开始执行线程 run() 定义线程 阅读全文
posted @ 2016-12-20 00:09 wilderness 阅读(476) 评论(0) 推荐(0)
摘要:一.关于Python多线程 Python解释器中可以同时运行多个线程,但是再任意时刻只能有一个线程在解释器运行。 Python虚拟机的访问是由全局解锁器(GIL)控制的,由GIL保证同时只有一个线程的运行。 执行方式如下: 1.设置GIL 2.切换进一个进程执行 3.执行下面操作中的一个 a.运行指 阅读全文
posted @ 2016-12-19 22:58 wilderness 阅读(7413) 评论(0) 推荐(0)
摘要:Determine whether an integer is a palindrome. Do this without extra space. 题意:确定一个数字是否回文 思路:把数字倒过来,与原数字对比 这是leetcode中做的最顺利的一个题了,一次ac,当然是因为这个题太简单了。。。 阅读全文
posted @ 2016-12-19 18:29 wilderness 阅读(151) 评论(0) 推荐(0)
摘要:Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below 阅读全文
posted @ 2016-12-19 12:38 wilderness 阅读(172) 评论(0) 推荐(0)
摘要:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 题意:反转数字 注意溢出 这是在LeetCode做的最简单的一个题了 阅读全文
posted @ 2016-12-18 17:09 wilderness 阅读(163) 评论(0) 推荐(0)
摘要:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font 阅读全文
posted @ 2016-12-18 16:19 wilderness 阅读(185) 评论(0) 推荐(0)
摘要:按照廖雪峰的官方网站http://www.liaoxuefeng.com/中给的步骤做的,但还是出现了一些问题: 1.安装MySQL时候,提示我没有安装Python3.4,我电脑安装的3.3,所以直接忽略了, 出现问题时候再说。 2.安装MySQL驱动器的时候,用下面这条命令失败,不知道原因 然后用 阅读全文
posted @ 2016-12-16 17:48 wilderness 阅读(184) 评论(0) 推荐(0)
摘要:设想我们要给一个student()类的一个实例s,添加一个score的属性,比如: s.score=999999 这个值明显是不合理的,但是它却是可行的,怎么能改变这种情况?我们能想到的就是用类方法 class student: def setsore: #code def getsocre: #c 阅读全文
posted @ 2016-12-16 16:33 wilderness 阅读(534) 评论(0) 推荐(0)
摘要:开一篇文章,记录关于Python有意思的用法,不断更新 1.Python树的遍历 阅读全文
posted @ 2016-12-14 20:39 wilderness 阅读(203) 评论(0) 推荐(0)
摘要:太晚了,明天有时间在写算法思路,先贴代码 ———————————————————————————————————————————————— 刚答辩完,毕业好难,感觉自己好水 ———————————————————————————————————————————————— 解题思路: 由低位到高位进行 阅读全文
posted @ 2016-12-14 00:04 wilderness 阅读(225) 评论(0) 推荐(0)
摘要:这篇文章包含了插入排序,希尔排序,堆排序,归并排序和快速排序,是前几篇文章的集合。 一共包括三个文件 sort.h sort.cpp main.cpp 1.main.cpp 2.sort.h 3.sort.cpp 阅读全文
posted @ 2016-12-14 00:00 wilderness 阅读(292) 评论(0) 推荐(0)
摘要:快速排序,平均运行时间O(N log N),最坏运行时间O(N^2)。 我觉得先看Python版的快排算法(http://www.cnblogs.com/fcyworld/p/6160558.html)比较容易理解。 整体思路: 首先从数组中选出一个值pivot,然后依据这个值pivot,把数组分成 阅读全文
posted @ 2016-12-13 23:54 wilderness 阅读(263) 评论(0) 推荐(0)
摘要:归并排序运行时间O(N log N),但是由于需要线性附加内存,所以很少用于主存排序。 算法核心在于以下三条语句,分治递归,分别对左半边和右半边的数组进行排序,然后把左右半边的数组一一进行比较放入数组 下面是代码,主要包括三个函数: 阅读全文
posted @ 2016-12-13 23:08 wilderness 阅读(249) 评论(0) 推荐(0)
摘要:堆排序,时间复杂度O(N log N),实际使用中慢于使用Sedgewick增量的增量排序。 大致思路: 1.先在数组中建堆,如果是增量排序,则需要建一个大堆 2.每循环一次,把最大的数,也就是nums[0],放入堆尾,同时把nums[0]下滤 阅读全文
posted @ 2016-12-13 22:20 wilderness 阅读(214) 评论(0) 推荐(0)
摘要:希尔排序(ShellSort),缩小增量排序,使用希尔增量时最坏运行时间O(n^2),不同的增量会对运行时间产生显著影响。 阅读全文
posted @ 2016-12-13 21:46 wilderness 阅读(140) 评论(0) 推荐(0)
摘要:今晚更新几个排序算法 插入排序,时间复杂度O(n^2),从前往后遍历,每遍历到一个值的时候,其前面的所有值已经完成排序,把这个值插入适当位置 阅读全文
posted @ 2016-12-13 21:30 wilderness 阅读(294) 评论(0) 推荐(0)
摘要:此题来自《数据结构与算法》,书中一共介绍了四种方法,这里贴出两种。 1.分治递归,对本题来说,虽然有更好的算法,但是用此题理解分治算法感觉挺有用 2.遍历整个数组,每个遍历的值保存入thissum,当thissum<0时,thissum置零;当thissum>max时,更新max 这种方法时间复杂度 阅读全文
posted @ 2016-12-12 23:52 wilderness 阅读(304) 评论(0) 推荐(0)
摘要:后天要中期答辩了,今天只刷了一个题,还没写出来,但是想更新博客,所以只有把之前写的东西贴出来了。 一个用于分区的shell脚本 阅读全文
posted @ 2016-12-12 23:41 wilderness 阅读(1763) 评论(0) 推荐(0)
摘要:Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm th 阅读全文
posted @ 2016-12-11 21:42 wilderness 阅读(1101) 评论(0) 推荐(0)
摘要:Python版的快排,使用递归。 1.设置递归终止条件,当元素个数<1时 2.从列表中pop出一个元素pv 3.列表中的剩余值与pv进行对比,大的放入smaller列表,小的放入larger列表 4.返回qs(smaller)+[pv]+qs(larger) 代码如下: 阅读全文
posted @ 2016-12-11 21:03 wilderness 阅读(1429) 评论(0) 推荐(0)
摘要:Python中,配置虚拟环境主要是为了防止版本之间的冲突,我是这么理解的: 1.用虚拟环境可以在一个电脑中使用多个Python解释器以及扩展; 2.可以方便的在同一台电脑中使用多个版本的代码。 虚拟环境的配置: 1.查看当前系统有没有可用的虚拟环境,如果报错,则需要安装虚拟环境 2.如果是在Cent 阅读全文
posted @ 2016-12-11 20:33 wilderness 阅读(862) 评论(0) 推荐(0)
摘要:Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may a 阅读全文
posted @ 2016-12-10 23:22 wilderness 阅读(199) 评论(0) 推荐(0)
摘要:Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected e 阅读全文
posted @ 2016-12-10 21:28 wilderness 阅读(226) 评论(0) 推荐(0)
摘要:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the 阅读全文
posted @ 2016-12-10 10:19 wilderness 阅读(149) 评论(0) 推荐(0)
摘要:Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order. Note: Example 1: Examp 阅读全文
posted @ 2016-12-10 00:18 wilderness 阅读(222) 评论(0) 推荐(0)
摘要:You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/ve 阅读全文
posted @ 2016-12-09 13:13 wilderness 阅读(242) 评论(0) 推荐(0)
摘要:这个题为《编程珠玑》中提到的算法,解题思路和桶排序/基数排序一样,适用于大量没有重复的数据。 结题思路: 1.遍历整个数据文件,每提取一个数据,在BitMap中对应的位置赋1 2.遍历BitMap的每一位,为1的位置上输出其再BitMap中的坐标 阅读全文
posted @ 2016-12-08 22:44 wilderness 阅读(315) 评论(0) 推荐(0)
摘要:Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters. Please note that the string 阅读全文
posted @ 2016-12-08 22:35 wilderness 阅读(230) 评论(0) 推荐(0)
摘要:Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. Y 阅读全文
posted @ 2016-12-08 21:21 wilderness 阅读(600) 评论(0) 推荐(0)
摘要:Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the leng 阅读全文
posted @ 2016-12-08 21:15 wilderness 阅读(193) 评论(0) 推荐(0)
摘要:首先想到的是走到其中一个链表的尽头,然后把剩余的链表中的值放入写的链表,返回,但是自己写的代码好长。 Python: 阅读全文
posted @ 2016-12-08 21:02 wilderness 阅读(175) 评论(0) 推荐(0)
摘要:1. 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 wou 阅读全文
posted @ 2016-12-08 20:56 wilderness 阅读(146) 评论(0) 推荐(0)