
Python
文章平均质量分 55
cheney康
中山大学计算机学院研究生
展开
-
Python手册
一、os.path以及sys.path模块简介(1).sys.path —— 动态地改变Python搜索路径如果python中导入的package或module不在环境变量PATH中,那么可以使用sys.path将要导入的package或module加入到PATH环境变量中。import sys sys.path.append(’引用模块的地址') #或者 import ...原创 2018-03-23 11:08:17 · 160 阅读 · 0 评论 -
python 文件操作(遍历、复制、生成文件名)
1、遍历文件夹下面所有文件,包括二级目录等打印出所有的文件名:import osdir = "文件夹路径"for root, dir, file in os.walk(dir): for b in file: print(b) # 只是打印文件名 print(dir,b) # 打印路径+文件名 2、只遍历文件夹下一级目录,打印出一级目录文件...原创 2018-07-16 21:25:43 · 4780 阅读 · 0 评论 -
运行.py 文件出现ImportError: No module named 'xxx'问题
问题描述:一般我们在pycharm中run程序的时候,会将当前工程的所有文件夹路径都作为包的搜索路径;而在命令行中运行‘xxx.py’时,或者sh脚本文件运行‘xxx.py’时,只是搜索当前路径,就会找不到module解决办法:在要运行的‘xxx.py’最前面加上以下代码:import sys,oscurPath = os.path.abspath(os.path.dirnam...原创 2018-07-24 20:01:57 · 5356 阅读 · 1 评论 -
python图像旋转
问题描述:python随机生成图像旋转任意角度后的图像,需要得到一张图像旋转任意角度之后的结果,随机生成10000张图像,并将输出的图像命名为角度值。代码实现:# -*- coding: utf-8 -*-import cv2from math import *import numpy as npimport random# 旋转angle角度,缺失背景白色(255,...原创 2018-07-30 17:37:37 · 2201 阅读 · 1 评论 -
【剑指offer python】面试题15:链表中倒数第k个结点
题目链接链表中倒数第k个结点题目描述输入一个链表,输出该链表中倒数第k个结点。solution 1遍历一次链表获得链表长度,再次遍历链表,至n-k+1出输出class Solution: def FindKthToTail(self, head, k): if head == None or k < 0: return...原创 2018-10-29 10:54:34 · 204 阅读 · 0 评论 -
【剑指offer python】面试题19:二叉树的镜像
题目链接二叉树的镜像题目描述操作给定的二叉树,将其变换为源二叉树的镜像。输入描述:二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...原创 2018-10-29 19:48:55 · 234 阅读 · 0 评论 -
【剑指offer python】数字在排序数组中出现的次数
题目链接数字在排序数组中出现的次数题目描述统计一个数字在排序数组中出现的次数。 solutionclass Solution: def GetNumberOfK(self, data, k): return data.count(k) ...原创 2018-10-30 08:29:29 · 247 阅读 · 0 评论 -
【剑指offer python】二叉树的深度
题目链接二叉树的深度题目描述输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。solutionclass Solution: def TreeDepth(self, pRoot): if not pRoot: return 0 else: ...原创 2018-10-30 08:45:31 · 193 阅读 · 0 评论 -
【剑指offer python】判断平衡二叉树
题目链接判断平衡二叉树题目描述输入一棵二叉树,判断该二叉树是否是平衡二叉树。solutionclass Solution: def TreeDepth(self,pRoot): if pRoot == None: return True return max(self.TreeDepth(pRoot.left) ...原创 2018-10-30 11:36:39 · 1145 阅读 · 1 评论 -
【剑指offer python】数组中只出现一次的数字
题目链接数组中只出现一次的数字题目描述一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。solutionclass Solution: # 返回[a,b] 其中ab是出现一次的两个数字 def FindNumsAppearOnce(self, array): l = [] for i ...原创 2018-10-30 16:21:47 · 227 阅读 · 0 评论 -
【剑指offer python】和为S的两个数字
题目链接和为S的两个数字题目描述输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。输出描述:对应每个测试案例,输出两个数,小的先输出。 思路:若array[left] + array[right] == tsum,就是答案(相差越远乘积越小)若array[left] + array[r...原创 2018-10-30 17:36:51 · 224 阅读 · 0 评论 -
【剑指offer python】左旋转字符串
题目链接左旋转字符串题目描述汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它! solutionclass Solution: ...原创 2018-10-30 19:16:03 · 259 阅读 · 0 评论 -
【剑指offer python】翻转单词顺序列
题目链接翻转单词顺序列题目描述牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助...原创 2018-10-30 19:25:43 · 327 阅读 · 0 评论 -
【剑指offer python】面试题52:构建乘积数组
链接:构建乘积数组题目描述给定一个数组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]。不能使用除法。解法一:去除A中序号为i的元素,其他元素乘积就是B[i]class Solution: def multiply(self, A): B = [1]...原创 2018-05-14 11:22:58 · 684 阅读 · 0 评论 -
【剑指offer python】青蛙跳台阶
链接:青蛙跳台阶题目描述一、一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。分析:假定第一次跳的是一阶,那么剩下的是n-1个台阶,跳法是f(n-1);假定第一次跳的是2阶,那么剩下的是n-2个台阶,跳法是f(n-2);假定第一次跳的是3阶,那么剩下的是n-3个台阶,跳法是f(n-3)......假定第一次跳的是n-1阶,...原创 2018-05-13 20:29:34 · 1611 阅读 · 0 评论 -
【LeetCode】728. Self Dividing Numbers
728. Self Dividing Numbershttps://leetcode.com/problems/self-dividing-numbers/description/A self-dividing number is a number that is divisible by every digit it contains.For example, 128 is a self-div...原创 2018-03-30 00:11:13 · 303 阅读 · 0 评论 -
【Leetcode】804. Unique Morse Code Words
804. Unique Morse Code Wordshttps://leetcode.com/problems/unique-morse-code-words/description/International Morse Code defines a standard encoding where each letter is mapped to a series of dots and d...原创 2018-03-27 00:12:53 · 1030 阅读 · 0 评论 -
Python 给某个文件名添加时间戳
问题描述:1、(先添加时间戳,再复制移动,两个文件加下面的文件名都被修改)将 /home/kangle/webdata/JPEGImages 路径下的111.jpg文件添加当前时刻的时间戳重命名为类似2018-03-27-18-11-11_111.jpg的形式,而且保存到另外一个路径/home/kangle/result下import datetimenowTime = datetime.dat...原创 2018-03-27 18:15:54 · 14175 阅读 · 0 评论 -
【Leetcode】771. Jewels and Stones
771. Jewels and StonesYou're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to kn...原创 2018-03-27 19:49:53 · 227 阅读 · 0 评论 -
【Leetcode】461. Hamming Distance
461. Hamming Distancehttps://leetcode.com/problems/hamming-distance/description/The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given...原创 2018-03-27 23:45:26 · 233 阅读 · 0 评论 -
【Leetcode】806. Number of Lines To Write String
806. Number of Lines To Write StringWe are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width...原创 2018-03-28 19:39:16 · 379 阅读 · 0 评论 -
【Leetcode】617. Merge Two Binary Trees
617. Merge Two Binary TreesGiven two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge th...原创 2018-03-28 22:39:28 · 182 阅读 · 0 评论 -
扇形涂色问题(Python)
问题描述:将一个圆形等分成N个小扇形,将这些扇形标记为1,2,3,…,N。现在使用M种颜色对每个扇形进行涂色,每个扇形涂一种颜色,且相邻的扇形颜色不同。求:有多少种涂色方法。 备注:不考虑数值越界。N>=1,M>=3;分析:设a(n)a(n)为符合要求的nn个扇形的涂色方法总和。 对扇形1有m种涂色方法,扇形2有m-1m-1种涂色方法,扇形3也有m-1m-1种涂色方法,扇形n也有m-1...原创 2018-04-12 20:15:10 · 1737 阅读 · 0 评论 -
【剑指offer python】面试题46:求1+2+…+n
链接:面试题46:求1+2+…+n题目描述求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。分析:直接调用python里的sum函数class Solution: def Sum_Solution(self, n): return sum(range(0,n+1))...原创 2018-05-17 16:07:49 · 343 阅读 · 0 评论 -
【剑指offer python】面试题39:二叉树的深度
题目描述输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.r...原创 2018-05-13 15:48:17 · 413 阅读 · 0 评论 -
【剑指offer python】面试题47:不用加减乘除做加法
题目描述写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。class Solution: def Add(self, num1, num2): # write code here while num2 != 0: temp = num1 ^ num2 num2 = (num1 &am...原创 2018-05-13 17:33:00 · 625 阅读 · 0 评论 -
【剑指offer python】面试题19:二叉树的镜像
题目描述操作给定的二叉树,将其变换为源二叉树的镜像。输入描述:二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5class So...原创 2018-05-13 17:52:23 · 232 阅读 · 0 评论 -
【剑指offer python】链表中环的入口结点
题目链接链表中环的入口结点题目描述给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。solutionclass Solution: def EntryNodeOfLoop(self, pHead): list = [] p = pHead while p: if p in ...原创 2018-11-02 21:57:24 · 200 阅读 · 0 评论