随笔分类 - Python
一门牛逼的语言
摘要:import curses import random import time import os class SnakeGame: def __init__(self): # 初始化颜色 self.init_colors() self.min_height = 10 self.min_width
阅读全文
摘要:使用PIL和numpy生成指定格式指定大小的图片,1GB以内已测试正常。 from PIL import Image import numpy as np # 指定目标图片最小体积 target_size_mb = 10 # 每个像素包含3个字节(RGB通道),计算需要的总像素数 bytes_per
阅读全文
摘要:通过精英保留策略简单解决基因退化的问题。 import random # 遗传算法参数 POP_SIZE = 10 # 种群大小 CHROM_LENGTH = 10 # 染色体长度(表示0-1023) CROSS_RATE = 0.7 # 交叉率 MUTATION_RATE = 0.01 # 变异率
阅读全文
摘要:需要指定 directConnection=true&authSource=atp-test 参数,,否则会报连接副本集超时。 在 PyMongo 中,directConnection 参数可以决定客户端是否直接连接到 MongoDB 服务器,而不是自动发现所有的副本集成员。当 directConn
阅读全文
摘要:docx类: import os from docx import Document from openpyxl import load_workbook def replace_string_in_docx(file_path, old_string, new_string): doc = Doc
阅读全文
摘要:想清楚了确实算是简单题. class Solution: def maxProfit(self, prices: List[int]) -> int: #1 if len(prices) == 1: return 0 #else max_profit = 0 min_price = prices[0
阅读全文
摘要:无敌的切片他又来了。 class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ #0 if k ==
阅读全文
摘要:排序,返回中值。 class Solution: def majorityElement(self, nums: List[int]) -> int: #always exists nums.sort() return nums[len(nums)//2]
阅读全文
摘要:原来leetcode使用Count也不需要import collections class Solution: def removeDuplicates(self, nums: List[int]) -> int: # len =0 if len(nums) == 0: return 0 # els
阅读全文
摘要:开刷Top Interview 150 class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nu
阅读全文
摘要:对于给定list: 1、若最高位个数大于2 ,返回最高位 2、若最高位个数不大于2,返回最高位和次高位 example: '''[5,4,3,2,1] -> [5,4][5,5,4,4,4] -> [5,5][5,5,5,4,4] -> [5,5,5][5,5,5,5,5] -> [5,5,5,5,
阅读全文
摘要:```Python#202506113 update import os root_dir = r"D:\Dir" old = "https://www.google.com/" new = "https://m.google.com/" for root, _, files in os.walk(
阅读全文
摘要:Success Details Success Runtime: 36 ms, faster than 80.76% of Python3 online submissions for Implement strStr(). Memory Usage: 13.9 MB, less than 12.3
阅读全文
摘要:Success Details Success Runtime: 52 ms, faster than 8.08% of Python3 online submissions for Remove Element. Memory Usage: 13.8 MB, less than 6.06% of
阅读全文
摘要:要求原位去重排序,返回nums的length并用nums的引用来检查排序,非常无聊的字符操作 不如来一发深拷贝吧~233 Success Details Runtime: 92 ms, faster than 90.98% of Python3 online submissions for Remo
阅读全文
摘要:从这位大佬这里抄了解法2:https://www.cnblogs.com/grandyang/p/4441324.html 解法二: 我们也可以使用递归来做,我们用 head 记录每段的开始位置,cur 记录结束位置的下一个节点,然后我们调用 reverse 函数来将这段翻转,然后得到一个 new_
阅读全文
摘要:这题很烦 做完满脑子都是箭头。 这里head之后 和tail之前 不能用next这种形式来给next复制 会造成循环链表 tailNode.next = headNode.next # 4 next point to 2 tailprevNode.next = headNode # 3 next p
阅读全文
摘要:在家瘫了半个月,虚弱时长2月半,又来水题了~ 内存感人,不过想不出来不用temp保存中间值的链表交换方法了。 Success Details Success Runtime: 40 ms, faster than 44.76% of Python3 online submissions for Sw
阅读全文
摘要:https://docs.python.org/zh-cn/3/tutorial/datastructures.html 列表推导式提供了一个更简单的创建列表的方法。常见的用法是把某种操作应用于序列或可迭代对象的每个元素上,然后使用其结果来创建列表,或者通过满足某些特定条件元素来创建子序列。 例如,
阅读全文

浙公网安备 33010602011771号