随笔分类 -  算法

python算法题
摘要: 阅读全文
posted @ 2022-06-11 15:07 狒狒桑 阅读(15) 评论(0) 推荐(0) 编辑
摘要:def func(s: str): temp = '' count = 0 for i in range(len(s)): for j in range(i + 1, len(s)): if s[i] != s[j]: break j += 1 if count < j - i: count = j 阅读全文
posted @ 2022-05-30 21:52 狒狒桑 阅读(21) 评论(0) 推荐(0) 编辑
摘要:(1)异或 ^ 特性: 1. 0 ^ N = N , N^N =0 2. A ^ N ^ A = N 3. 支持交换律和结合律 4. 记忆方法:二进制按位相加不进位 交换: a = a ^ b b = a ^ b a = a ^ b (2)加减交换 a = a + b b = a - b a = a 阅读全文
posted @ 2022-05-26 18:43 狒狒桑 阅读(41) 评论(0) 推荐(0) 编辑
摘要:# 求子序列最大平均数def func(li: list, k: int): sum = 0 n = len(li) for i in range(k): sum += li[i] for i in range(k, n): temp = sum + li[i] - li[k - i] if tem 阅读全文
posted @ 2022-05-14 16:11 狒狒桑 阅读(19) 评论(0) 推荐(0) 编辑
摘要:# 排列硬币# 暴力求解def func1(n: int): for i in range(1, n + 1): n -= i if n < i: return i# 二分求解def func2(n: int): low = 1 high = n while low <= high: mid = ( 阅读全文
posted @ 2022-05-14 14:53 狒狒桑 阅读(27) 评论(0) 推荐(0) 编辑
摘要:# 暴力求解def func1(num: int): if num < 2: return num return func1(num - 1) + func1(num - 2)# 去重队列def recurse(li: list, num: int): if num == 0: return 0 i 阅读全文
posted @ 2022-05-14 11:48 狒狒桑 阅读(44) 评论(0) 推荐(0) 编辑
摘要:# 暴力算法def func1(li: list, target: int): for i in range(len(li)): for j in range(i + 1, len(li)): if li[i] + li[j] == target: return i, j return -1, -1 阅读全文
posted @ 2022-05-14 10:34 狒狒桑 阅读(61) 评论(0) 推荐(0) 编辑
摘要:# 求三个数最大乘积# 思路:三个数都是正数,则取最大三个数;三个数都是复数,也取最大三个数;有正有负,取最小的2个负数、最大的正数# 排序求解from math import infdef func1(li:list): li.sort() return max(li[0]*li[1]*li[-1 阅读全文
posted @ 2022-05-13 23:37 狒狒桑 阅读(167) 评论(0) 推荐(0) 编辑
摘要:# x平方根# 暴力求解def func1(x: int): i = 1 while True: if i * i < x: i += 1 elif i * i == x: return i else: return i - 1# 二分查找def func2(x: int): index = 0 l 阅读全文
posted @ 2022-05-13 23:11 狒狒桑 阅读(24) 评论(0) 推荐(0) 编辑
摘要:def fun1(li: list): if len(li) == 1: return 1 sum_num = sum(li) res = 0 for i in range(len(li)): res += li[i] if res == sum_num: return i sum_num -= l 阅读全文
posted @ 2022-05-13 22:20 狒狒桑 阅读(17) 评论(0) 推荐(0) 编辑
摘要:# 有序数列def fun1(li: list): return len(set(li))def fun2(li: list): new = [] for i in li: if i not in new: new.append(i) return len(new)def fun3(li: list 阅读全文
posted @ 2022-05-13 19:35 狒狒桑 阅读(30) 评论(0) 推荐(0) 编辑
摘要:# 判断是否素数def premi(n): if n < 2: return False import math for i in range(2, math.floor(math.sqrt(n)) + 1): if n % i == 0: return False return True# 统计素 阅读全文
posted @ 2022-05-13 19:05 狒狒桑 阅读(33) 评论(0) 推荐(0) 编辑
摘要:# 冒泡排序def mp_sort(li: list): slen = len(li) for i in range(slen - 1): for j in range(slen - i - 1): if li[j] > li[j + 1]: li[j], li[j + 1] = li[j + 1] 阅读全文
posted @ 2022-05-07 10:15 狒狒桑 阅读(35) 评论(0) 推荐(0) 编辑
摘要:def func1(n, base): convertion = '0123456789ABCDE' if n < base: return convertion[n] else: return func1(n // base, base) + convertion[n % base]def fun 阅读全文
posted @ 2022-04-30 15:33 狒狒桑 阅读(43) 评论(0) 推荐(0) 编辑
摘要:# 判断是否是回文def func1(s: str): s1 = list(s) while len(s1) > 1: if s1.pop() != s1.pop(0): return False return True# 求子串中最长回文def func2(s: str): s1 = list(s 阅读全文
posted @ 2022-04-30 13:11 狒狒桑 阅读(23) 评论(0) 推荐(0) 编辑
摘要:# 打印机设置class Printer: def __init__(self, ppm): self.pagerate = ppm self.currentTask = None self.timeRemaining = 0 def tick(self): if self.currentTask 阅读全文
posted @ 2022-04-30 12:13 狒狒桑 阅读(57) 评论(0) 推荐(0) 编辑
摘要:# 热土豆/约瑟夫问题def func1(s: list, n: int): temp = [] while len(s) > 0: if len(s) > 1: for i in range(n): s.append(s.pop(0)) temp.append(s.pop()) return te 阅读全文
posted @ 2022-04-30 11:03 狒狒桑 阅读(32) 评论(0) 推荐(0) 编辑
摘要:# 中缀表达式转为后缀表达式def func1(s: str): s1temp = [] s2temp = [] for i in s.split(): if i.isdigit(): s1temp.append(i) elif i in '*/+-': s2temp.append(i) elif 阅读全文
posted @ 2022-04-30 10:20 狒狒桑 阅读(22) 评论(0) 推荐(0) 编辑
摘要:# 十进制转化为N进制def func1(s: str, s_base:int, n_base: int): baselist = '0123456789ABCDEF' res = str(s) # 原数不是十进制时,先转为十进制:累加 if s_base != 10: ssum = 0 s = s 阅读全文
posted @ 2022-04-28 18:52 狒狒桑 阅读(29) 评论(0) 推荐(0) 编辑
摘要:# 匹配()def func1(s: str): temp = [] for i in s: if i in '(': temp.append(i) else: if i in ')': if temp: temp.pop() else: return False if temp: return F 阅读全文
posted @ 2022-04-28 17:56 狒狒桑 阅读(21) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示