随笔分类 -  Python

一门牛逼的语言
摘要:import curses import random import time import os class SnakeGame: def __init__(self): # 初始化颜色 self.init_colors() self.min_height = 10 self.min_width 阅读全文
posted @ 2025-02-20 09:57 夜歌乘年少 阅读(52) 评论(0) 推荐(0)
摘要:使用PIL和numpy生成指定格式指定大小的图片,1GB以内已测试正常。 from PIL import Image import numpy as np # 指定目标图片最小体积 target_size_mb = 10 # 每个像素包含3个字节(RGB通道),计算需要的总像素数 bytes_per 阅读全文
posted @ 2024-12-17 11:08 夜歌乘年少 阅读(98) 评论(0) 推荐(0)
摘要:通过精英保留策略简单解决基因退化的问题。 import random # 遗传算法参数 POP_SIZE = 10 # 种群大小 CHROM_LENGTH = 10 # 染色体长度(表示0-1023) CROSS_RATE = 0.7 # 交叉率 MUTATION_RATE = 0.01 # 变异率 阅读全文
posted @ 2024-09-05 14:38 夜歌乘年少 阅读(99) 评论(0) 推荐(0)
摘要:需要指定 directConnection=true&authSource=atp-test 参数,,否则会报连接副本集超时。 在 PyMongo 中,directConnection 参数可以决定客户端是否直接连接到 MongoDB 服务器,而不是自动发现所有的副本集成员。当 directConn 阅读全文
posted @ 2024-08-14 17:06 夜歌乘年少 阅读(90) 评论(0) 推荐(1)
摘要: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 阅读全文
posted @ 2024-07-04 14:37 夜歌乘年少 阅读(93) 评论(0) 推荐(0)
摘要:想清楚了确实算是简单题. class Solution: def maxProfit(self, prices: List[int]) -> int: #1 if len(prices) == 1: return 0 #else max_profit = 0 min_price = prices[0 阅读全文
posted @ 2024-07-02 00:28 夜歌乘年少 阅读(22) 评论(0) 推荐(0)
摘要:无敌的切片他又来了。 class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ #0 if k == 阅读全文
posted @ 2024-07-01 16:22 夜歌乘年少 阅读(34) 评论(0) 推荐(0)
摘要:排序,返回中值。 class Solution: def majorityElement(self, nums: List[int]) -> int: #always exists nums.sort() return nums[len(nums)//2] 阅读全文
posted @ 2024-06-30 14:16 夜歌乘年少 阅读(16) 评论(0) 推荐(0)
摘要:原来leetcode使用Count也不需要import collections class Solution: def removeDuplicates(self, nums: List[int]) -> int: # len =0 if len(nums) == 0: return 0 # els 阅读全文
posted @ 2024-06-30 12:58 夜歌乘年少 阅读(21) 评论(0) 推荐(0)
摘要:开刷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 阅读全文
posted @ 2024-06-30 11:08 夜歌乘年少 阅读(27) 评论(0) 推荐(0)
摘要:对于给定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, 阅读全文
posted @ 2020-10-24 17:12 夜歌乘年少 阅读(153) 评论(0) 推荐(0)
摘要:```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( 阅读全文
posted @ 2020-01-10 22:21 夜歌乘年少 阅读(1925) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2019-08-12 10:57 夜歌乘年少 阅读(168) 评论(0) 推荐(0)
摘要: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  阅读全文
posted @ 2019-08-12 10:22 夜歌乘年少 阅读(149) 评论(0) 推荐(0)
摘要:要求原位去重排序,返回nums的length并用nums的引用来检查排序,非常无聊的字符操作 不如来一发深拷贝吧~233 Success Details Runtime: 92 ms, faster than 90.98% of Python3 online submissions for Remo 阅读全文
posted @ 2019-08-09 14:35 夜歌乘年少 阅读(294) 评论(1) 推荐(0)
摘要:从这位大佬这里抄了解法2:https://www.cnblogs.com/grandyang/p/4441324.html 解法二: 我们也可以使用递归来做,我们用 head 记录每段的开始位置,cur 记录结束位置的下一个节点,然后我们调用 reverse 函数来将这段翻转,然后得到一个 new_ 阅读全文
posted @ 2019-08-08 16:56 夜歌乘年少 阅读(178) 评论(0) 推荐(0)
摘要:这题很烦 做完满脑子都是箭头。 这里head之后 和tail之前 不能用next这种形式来给next复制 会造成循环链表 tailNode.next = headNode.next # 4 next point to 2 tailprevNode.next = headNode # 3 next p 阅读全文
posted @ 2019-08-08 16:06 夜歌乘年少 阅读(169) 评论(0) 推荐(0)
摘要:在家瘫了半个月,虚弱时长2月半,又来水题了~ 内存感人,不过想不出来不用temp保存中间值的链表交换方法了。 Success Details Success Runtime: 40 ms, faster than 44.76% of Python3 online submissions for Sw 阅读全文
posted @ 2019-08-08 10:36 夜歌乘年少 阅读(212) 评论(0) 推荐(0)
摘要:result: 阅读全文
posted @ 2019-07-09 15:41 夜歌乘年少 阅读(568) 评论(0) 推荐(0)
摘要:https://docs.python.org/zh-cn/3/tutorial/datastructures.html 列表推导式提供了一个更简单的创建列表的方法。常见的用法是把某种操作应用于序列或可迭代对象的每个元素上,然后使用其结果来创建列表,或者通过满足某些特定条件元素来创建子序列。 例如, 阅读全文
posted @ 2019-06-06 17:34 夜歌乘年少 阅读(372) 评论(0) 推荐(0)