![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python
仝笛
这个作者很懒,什么都没留下…
展开
-
0/1背包问题 python
0/1背包问题 python有一个包和n个物品,包的容量为m,每个物品都有各自的体积和价值,问当从这n个物品中选择多个物品放在包里而物品体积总数不超过包的容量m时,能够得到的最大价值是多少?'''测试数据:n = 6 物品的数量,c = 10 书包能承受的重量,w = [2, 2, 3, 1, 5, 2] 每个物品的重量,v = [2, 3, 1, 5, 4, 3] 每个物品的价值...原创 2019-09-06 16:02:03 · 436 阅读 · 0 评论 -
python * **
# -*- coding: utf-8 -*-"""# *可以用来解包#这里还不知道args是什么,就加前缀,那args就作为一个整体#即args是一个可解包的变量,再加上不可修改,那就是一个元组了!!def F1(*args): print(args)F1(123,"456")# (123, '456')#** 姑且看作两次解包,那么需要进行两次解包的数据结构,只...原创 2019-08-22 00:32:21 · 257 阅读 · 0 评论 -
leecode70. 爬楼梯
leecode 70. 爬楼梯题目描述:假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?注意:给定 n 是一个正整数。示例 1:输入: 2输出: 2解释: 有两种方法可以爬到楼顶。1 阶 + 1 阶2 阶示例 2:输入: 3输出: 3解释: 有三种方法可以爬到楼顶。1 阶 + 1 阶 + 1 阶...原创 2019-08-24 12:00:18 · 131 阅读 · 0 评论 -
句子的逆序
题目:对于一个字符串,请设计一个算法,只在字符串的单词间做逆序调整,也就是说,字符串由一些由空格分隔的部分组成,你需要将这些部分逆序。给定一个原字符串A和他的长度,请返回逆序后的字符串。测试样例:“dog loves pig”,13返回:“pig loves dog”# -*- coding:utf-8 -*-class Reverse: def reverseSente...原创 2019-08-24 11:35:44 · 86 阅读 · 0 评论 -
旋转词
题目:如果对于一个字符串A,将A的前面任意一部分挪到后边去形成的字符串称为A的旋转词。比如A=“12345”,A的旋转词有"12345",“23451”,“34512”,“45123"和"51234”。对于两个字符串A和B,请判断A和B是否互为旋转词。给定两个字符串A和B及他们的长度lena,lenb,请返回一个bool值,代表他们是否互为旋转词。测试样例:“cdab”,4,“abcd”,...原创 2019-08-24 11:06:53 · 421 阅读 · 0 评论 -
pandas 读csv文件,报错:ParserError:Error tokenizing data.
pandas 读csv文件,报错:ParserError: Error tokenizing data. C error: Buffer overflow caught - possible malformed input file.解决方法1:如果使用python及其大文件,可以使用 engine=‘python’解决方法2:原因是pandas用作行终止符的数据中有一些回车符“\ r...原创 2019-08-15 12:31:01 · 3102 阅读 · 0 评论 -
leecode104. Maximum Depth of Binary Tree树高
leecode104. Maximum Depth of Binary Tree树高Example:Given binary tree [3,9,20,null,null,15,7],*** 3*** / \** 9 20**** / \** 15 7return its depth = 3.#迭代class Solution: def maxDep...原创 2019-07-07 15:07:51 · 98 阅读 · 0 评论 -
leecode10. Regular Expression Matching字符串匹配
leecode10. Regular Expression Matching字符串匹配‘.’ Matches any single character.‘*’ Matches zero or more of the preceding element.Example 1:Input:s = “aa”p = “a”Output: falseExplanation: “a” does ...原创 2019-07-06 23:50:54 · 135 阅读 · 0 评论 -
leecode103. Binary Tree Zigzag Level Order Traversal二叉树 之字形 层次遍历
Binary Tree Zigzag Level Order TraversalFor example:Given binary tree [3,9,20,null,null,15,7],3/ \9 20** / \15 7return its zigzag level order traversal as:[[3],[20,9],[15,7]]class...原创 2019-07-06 19:18:25 · 126 阅读 · 0 评论 -
leecode102. Binary Tree Level Order Traversal二叉树层次遍历
leecode102. Binary Tree Level Order Traversal二叉树层次遍历For example:Given binary tree [3,9,20,null,null,15,7],3/ \9 20** / \*15 7return its level order traversal as:[[3],[9,20],[15,7]]# ...原创 2019-07-06 18:09:51 · 231 阅读 · 0 评论 -
leecode 101. Symmetric Tree对称树
leecode 101. Symmetric Tree对称树For example, this binary tree [1,2,2,3,4,4,3] is symmetric:***1** / \** 2 2** / \ / \*3 4 4 3But the following [1,2,2,null,3,null,3] is not:1/ \2 2\ ...原创 2019-07-06 17:51:04 · 121 阅读 · 0 评论 -
leecode 100. Same Tree
Same TreeExample 1:Input: 1 1------- / \ / \*****2 3 2 3 [1,2,3], [1,2,3]Output: trueExample 2:Input: 1 ***1------- / \ ** / \******2 ...原创 2019-07-06 17:30:16 · 83 阅读 · 0 评论 -
np.newaxis
np.newaxis可以给数组加一维import numpy as npdata = np.array([range(9),range(9,18)])print(data) # shape (2,9)#[[ 0 1 2 3 4 5 6 7 8]# [ 9 10 11 12 13 14 15 16 17]]print(data[:,:,np.newaxis]) # ...原创 2019-08-22 15:02:00 · 106 阅读 · 0 评论 -
python ASCII码与字符相互转换
python ASCII码与字符相互转换'''ord(str)输入长度为1 的str输出 int 其ASCII码 chr(int)输入 int输出 str ASCII码对应的字符'''print(ord('a'))# 97print(chr(97))# a原创 2019-08-26 19:53:41 · 443 阅读 · 0 评论 -
LeetCode56. 合并区间
LeetCode56. 合并区间Given a collection of intervals, merge all overlapping intervals.Example 1:Input: [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and ...原创 2019-09-04 22:04:32 · 178 阅读 · 0 评论 -
python 方法和函数有什么区别?
python 方法和函数有什么区别?直接上代码:from types import FunctionType,MethodTypeclass C: def func1(self): passdef func2(): passprint("func1是函数", isinstance(C().func1, FunctionType))#Falseprint...原创 2019-09-04 20:55:59 · 312 阅读 · 0 评论 -
python sort(),sorted()的参数key
python sort(),sorted()的参数keyarray = [1,2,8,4,5,8,3,5,-1]#key参数,要指向一个函数,返回该计算方法得到的、排序用的权值.print(sorted(array,key=lambda x:x))#[-1, 1, 2, 3, 4, 5, 5, 8, 8]print(sorted(array,key=lambda x:-x))#[8, 8,...原创 2019-09-04 15:31:18 · 453 阅读 · 0 评论 -
python any() all()
python any() all()# any(x):判断 x 对象是否为空对象,如果都为空、0、false,则返回 false#如果不都为空、0、false,则返回 true#orprint(any([0,8]),any([10]))#Tureprint(any([0,None]),any(['']),any([False]),any([]))#False#all(x):如果 al...原创 2019-09-04 15:08:04 · 96 阅读 · 0 评论 -
Python 中递归的最大次数
Python 中递归的最大次数可以使用sys.setrecursionlimit来调整#windows10count=10000temp=[]def fun():# print(count) temp.append(1) if count>0: fun()fun()print(len(temp))#2966#RecursionErro...原创 2019-09-04 14:02:45 · 625 阅读 · 0 评论 -
__init__.py 有什么作用?
init.py 有什么作用?例如:一个包里有三个模块,model1.py、model2.py、model3.py,但使用 from models import *导入模块时,如何保证只有 model1、model3被导入了?在文件 init.py中增加:all = [‘model1’,‘model3’]...原创 2019-09-04 13:33:11 · 318 阅读 · 0 评论 -
leecode13. Roman to Integer
leecode13. Roman to Integer题目描述罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。字符 数值I 1V 5X 10L 50C 100D 500M 10...原创 2019-09-04 10:51:08 · 92 阅读 · 0 评论 -
leetCode303 数组范围求和
leetCode303 数组范围求和题目描述:给定一个数组,求出数组范围 i 到 j (i ≤ j) 的元素总和,包含 i, j 两点。例如:给定nums = [-2, 0, 3, -5, 2, -1],?求和函数为sumRange()sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRange(0, 5) -> -3#简单,但...原创 2019-09-03 16:59:58 · 258 阅读 · 0 评论 -
Leetcode 121:买卖股票的最佳时机
Leetcode 121:买卖股票的最佳时机题目描述:给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。注意你不能在买入股票前卖出股票。示例 1:输入: [7,1,5,3,6,4]输出: 5解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的...原创 2019-09-03 16:03:29 · 83 阅读 · 0 评论 -
leetcode 53. 最大子序和
leetcode 53. 最大子序和给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。示例:输入: [-2,1,-3,4,-1,2,1,-5,4],输出: 6解释: 连续子数组 [4,-1,2,1] 的和最大,为 6class Solution: def maxSubArray(self, nums: List[int]) -...原创 2019-09-01 18:45:48 · 72 阅读 · 0 评论 -
LeetCode122:卖股票的最佳时机 II
LeetCode122:卖股票的最佳时机 II题目描述:给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。示例 1:输入: [7,1,5,3,6,4]输出: 7解释: 在第 2 天(股票价格 = 1)的时候买入,...原创 2019-09-01 18:36:48 · 110 阅读 · 0 评论 -
leecode98. Validate Binary Search Tree验证搜索二叉树
Example 1:2/ \1 3Input: [2,1,3]Output: trueExample 2:5/ \1 4– / \*3 6Input: [5,1,4,null,null,3,6]Output: false# Definition for a binary tree node.# class TreeNode:# def _...原创 2019-07-06 16:33:51 · 88 阅读 · 0 评论 -
快排python 递归和迭代
quick_sort = lambda array: array if len(array) <= 1 else quick_sort([item for item in array[1:] if item <= array[0]]) + [array[0]] + + quick_sort([item for item in array[1:] if item > ar...原创 2019-07-06 16:02:19 · 155 阅读 · 0 评论 -
leecode 5. Longest Palindromic Substring最长回文子串
leecode 5. Longest Palindromic Substring最长回文子串Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example 1:Input: “babad”Output: “ba...原创 2019-06-30 15:19:06 · 144 阅读 · 0 评论 -
leecode46 全排列
给定一个没有重复数字的序列,返回其所有可能的全排列。示例:输入: [1,2,3]输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]思路1 : 库函数Python3 itertools 文档 :https://docs.python.org/3/library/itertools.html#itertools.permu...原创 2019-05-27 21:38:37 · 157 阅读 · 0 评论 -
leecode7. 整数反转
题目描述:给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。示例 1:输入: 123输出: 321示例 2:输入: -123输出: -321示例 3:输入: 120输出: 21注意:假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。解决方法class...原创 2019-05-18 17:42:12 · 111 阅读 · 0 评论 -
leecode 887. 鸡蛋掉落
题目描述你将获得 K 个鸡蛋,并可以使用一栋从 1 到 N 共有 N 层楼的建筑。每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去。你知道存在楼层 F ,满足 0 <= F <= N 任何从高于 F 的楼层落下的鸡蛋都会碎,从 F 楼层或比它低的楼层落下的鸡蛋都不会破。每次移动,你可以取一个鸡蛋(如果你有完整的鸡蛋)并把它从任一楼层 X 扔下(满足 1 <=...原创 2019-05-18 16:45:28 · 75 阅读 · 0 评论 -
python 阶乘math.factorial
print(math.factorial(3))#3*2=6原创 2019-05-18 15:17:28 · 7338 阅读 · 0 评论 -
leecode 5. 最长回文子串
题目描述:给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。示例 1:输入: “babad”输出: “bab”注意: “aba” 也是一个有效答案。示例 2:输入: “cbbd”输出: “bb”中心拓展算法class Solution: def longestPalindrome(self, s: str) -> str: ...原创 2019-05-18 14:56:56 · 101 阅读 · 0 评论 -
Fast R-CNN总结
Fast R-CNN(1)输入测试图像;(2)利用selective search 算法在图像中从上到下提取2000 个左右的建议窗口(Region Proposal) ;(3)将 整张图片 输入CNN,进行特征提取;(4)把 建议窗口 映射 到CNN的 最后一层卷积feature map 上;(5)通过RoI pooling层使每个建议窗口 生成固定尺寸的feature map ;...转载 2019-05-27 17:28:35 · 233 阅读 · 0 评论 -
R-CNN总结
R-CNN论文发布时间是 2014 年在过去的十多年时间里,传统的机器视觉领域,通常采用特征描述子来应对目标识别任务,这些特征描述子最常见的就是 SIFT 和 HOG.利用候选区域与 CNN 结合做目标定位借鉴了 滑动窗口 思想,R-CNN 采用对区域进行识别的方案。具体是:1、给定一张输入图片,从图片中提取 2000 个类别独立的候选区域。2、对于每个区域利用 CNN 抽取一个固定...转载 2019-05-24 18:18:23 · 351 阅读 · 0 评论 -
牛客网 二叉搜索树的第k个结点
二叉搜索树的第k个结点题目描述给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = ...原创 2019-05-07 16:22:56 · 103 阅读 · 0 评论 -
牛客网 把数组排成最小的数
题目描述输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。# -*- coding:utf-8 -*-#利用 sorted 设置比较方法lambdaclass Solution: def PrintMinNumber(self, numbers): ...原创 2019-05-07 15:46:03 · 95 阅读 · 0 评论 -
DeBug Python 工具PySnooper
安装命令:#不知为何 conda安装不行 0.0#pip install pysnooperpip3 install pysnooperTestimport pysnooper#这句放在要测试的函数前,乱放会报错@pysnooper.snoop()def print_test(n): for i in range(n): print('now is:' ,...原创 2019-04-27 19:03:11 · 198 阅读 · 0 评论 -
Leetcode836 矩形重叠
矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标。如果相交的面积为正,则称两矩形重叠。需要明确的是,只在角或边接触的两个矩形不构成重叠。给出两个矩形,判断它们是否重叠并返回结果。示例 1:输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3]输出:true示例 2:输入...原创 2019-04-26 19:28:43 · 165 阅读 · 0 评论 -
leecode48. 旋转图像
题目描述给定一个 n × n 的二维矩阵表示一个图像。将图像顺时针旋转 90 度。说明:你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。示例 1:给定 matrix =[[1,2,3],[4,5,6],[7,8,9]],原地旋转输入矩阵,使其变为:[[7,4,1],[8,5,2],[9,6,3]]示例 2:给定 ma...原创 2019-05-27 21:58:51 · 116 阅读 · 0 评论