
Python
文章平均质量分 59
monkeyduck
清华大学CS在读,研究领域为语音处理、模式识别、对话管理,依然在不断学习中,成功在于点滴积累!
展开
-
‘dict’ object has no attribute 'has_key'解决办法
最近开始学习Python,安装上最新的Python3.3.3照书敲了一个小程序结果报错‘dict’ object has no attribute 'has_key'上网查也找不到解决办法,后来发现时Python版本太新的原因!Python3以后删除了has_key()方法!解决办法:1、重新安装个Python,推荐2.7.6,用的人多些。好多人不习惯用3,仍然在用22、修改代原创 2014-01-08 16:59:15 · 80841 阅读 · 2 评论 -
【LeetCode】【Python题解】Rotate Image
一行解法以及酷炫解法原创 2014-09-20 15:11:23 · 4804 阅读 · 0 评论 -
Python、C++中编写函数以及不使用临时变量交换两变量值的比较
在C++中,交换两变量的值一般第一反应都会是如下的代码:{temp=a;a=b;b=temp}但是题目要求不允许使用临时变量temp呢?可能就需要想一会了。但也有解决办法:{ b=a+b; a=b-a; b=b-a;}这种做法在a和b数值较小的情况下是对的,但是a+b如果很大则会溢出,所以也不是最优的解法。最好的方法是使用异或:{a ^= b原创 2014-09-21 14:25:26 · 2632 阅读 · 2 评论 -
【LeetCode】【Python】Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2014-09-11 18:00:03 · 2872 阅读 · 0 评论 -
【LeetCode】【Python题解】Pascal's Triangle
仅用一行实现!原创 2014-09-13 23:38:32 · 3448 阅读 · 1 评论 -
【LeetCode】【Python题解】Single NumberII
非常巧妙地解法!原创 2014-09-15 23:08:07 · 2557 阅读 · 0 评论 -
【LeetCode】【Python题解】Container with most water
这道题真真是在面试中碰到过,可惜当时复杂度到O(n2)了,太挫了,怪不得没有通过面试。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 en原创 2014-10-08 21:30:05 · 4435 阅读 · 2 评论 -
【LeetCode】【Python】Minimum Path Sum
此题不难,可以用dfs来做,也可以用动态规划,但明显dfs性能不如dp。Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note:原创 2014-10-09 12:22:40 · 1898 阅读 · 0 评论 -
【LeetCode】【C++】Linked list cycle 2
面试经常问的一道算法题!原创 2014-10-14 20:28:47 · 2605 阅读 · 0 评论 -
Hard??没搞错吧,LeetCode水题一道:Find Minimum in Rotated Sorted Array II
做完Find Minimum in Rotated Sorted Array I觉得很简单,再做2发现也不难,但是LeetCode竟然给了一个hard评级,难道是我编程水平大有长进?哈哈我估计是对复杂度有很高的要求的情况下比较难吧。。Python(偷懒)做法就是一句话:return min(num)Java(基本做法)如下:public class Solution { pub原创 2015-03-03 10:48:23 · 2173 阅读 · 0 评论 -
Django1.8 更新数据库
最近在搭个网站,扩展了user后需要同步更改到数据库(我用的sqlite3),上网查了n多,网上都是千篇一律,转来转去的,很是讨厌!! 不知道是不是1.8才有的这个功能,更新数据表只需要执行python manage.py makemigrations此时会出提示信息Migrations for 'registration': 0002_userprofile.py: - Create原创 2015-07-24 22:43:48 · 2871 阅读 · 1 评论 -
【LeetCode】Balanced Tree & Binary Search Tree
整合两道差不多的题目放上来,其中第一题是第二题的基础。1.Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two原创 2014-09-06 20:24:58 · 2787 阅读 · 0 评论 -
【Python】分享一个大牛的Python教程
链接:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000#0,收好不谢!其中Git教程也很不错,简明易懂还实用,下一步我打算将我的口语对话系统在GitHub上开源!原创 2014-06-20 13:37:25 · 2356 阅读 · 2 评论 -
【LeetCode】【Python】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3},比较简单,就是转化成中序遍历即可,访问顺序是中序遍历左子树,根节点,中序遍历右子树Python编程的时候需要注意,要在返回单一数字的时候加原创 2014-07-25 09:34:45 · 3548 阅读 · 0 评论 -
【Python】set与frozenset的区别
同多数语言一样,set表示集合,最重要的特性就是无序,所以Python中的set不支持indexing,但支持len(set),x in set 等操作。set有两种类型,set和frozenset。set是可变的,有add(),remove()等方法。既然是可变的,所以它不存在哈希值。frozenset是冻结的集合,它是不可变的,存在哈希值,好处是它可以作为字典的key,也可以作为其原创 2014-01-09 11:32:08 · 12699 阅读 · 3 评论 -
Django admin界面丢失CSS解决办法
跟着教程一步步的做却遇到了显示界面太丑陋的问题,就是白纸黑字,毫无美感。如下图当然看教程的评论说取消掉setting.py里的django.contrib.staticfiles的注释即可,可是我取消了却一点用也没用。这应该和python的版本有关系。我是windows系统,python是2.7.6,django是1.6.1的,据说python3.x不会遇到css丢失问题,因为py原创 2014-02-24 16:00:23 · 4434 阅读 · 0 评论 -
正则表达式入门级语法
刚学正则表达式,整理一下最最基础的语法符号匹配. (dot)任意单一字符\d任意一位数字[A-Z]A 到 Z中任意一个字符(大写)[a-z]a 到 z中任意一个字符(小写)[A-Za-z]a 到 z中任意一个字符(不区分大小写)原创 2014-02-17 17:14:52 · 1055 阅读 · 0 评论 -
Django 发送email配置详解及各种错误类型
跟随Django Book的内容发送邮件不成功,总结一下需要配置好settings.py文件,还要注意一些细节。1、在settings文件最后添加以下内容,缺一不可!EMAIL_HOST= 'smtp.163.com'EMAIL_PORT= 25EMAIL_HOST_USER = 'xxxxxx@163.com'(你有163邮箱的话)EMAIL_HOST_PASSWORD =原创 2014-03-03 21:34:39 · 10773 阅读 · 0 评论 -
【LeetCode】【Python题解】Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.比较两个原创 2014-07-20 20:01:48 · 3937 阅读 · 0 评论 -
【LeetCode】【Python题解】Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before c原创 2014-07-20 20:06:48 · 6789 阅读 · 2 评论 -
【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree
今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的,因为c++版的代码网上比较多,所以就只分享一下Python的代码吧,刚学完Python的基本语法,做做LeetCode的题目还是不错的,对以后找工作面试也有帮助!刚开始就从AC率最高的入手吧!1.Given an array of integers, every element appe原创 2014-07-20 13:18:24 · 4140 阅读 · 0 评论 -
【LeetCode】【Python题解】Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on原创 2014-07-24 10:11:44 · 4933 阅读 · 0 评论 -
【LeetCode】【Python题解】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \原创 2014-07-24 10:27:20 · 4081 阅读 · 0 评论 -
【LeetCode】【Python】Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:笨办法是每个节点再开辟一个属性存放是否访问过,这样遍历一遍即可知道是否有环。但为了不增加额外的空间,可以设置两个指针,一个一次走一步,另一个一次走两步原创 2014-07-25 09:24:25 · 2130 阅读 · 0 评论 -
Django1.6 用Form实现注册登录注销修改密码(含代码!)
参照了潘军杰的博客,但他Django版本有点老了,改动了不少东西,总体来说参考价值还是很大的,点http://www.2goo.info/blog/panjj/Django/2010/06/05/94跳转按上Django的可以去目录下找auth这个包,它涵盖了登录注册的大部分功能,我们拿过来直接用就可以了。我Python按在了D盘,所以目录就是这个,D:\Python2.7.6\Lib\sit原创 2014-03-19 16:14:32 · 6868 阅读 · 6 评论