![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
ailinyingai
DevOps 搬砖人 干饭人
展开
-
297 序列化和反序列化
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Codec { String SEP=","; String NULL="#"; // Encodes a原创 2021-01-23 19:39:04 · 112 阅读 · 0 评论 -
0023
# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def mergeKLists(self, lists: List[ListNode]) -> ListNode: if not lists: return n = len(lis原创 2021-01-21 21:15:49 · 75 阅读 · 1 评论 -
矩阵旋转
def rotate_matrix(matrix): """rotates a matrix 90 degrees clockwise""" n = len(matrix) for layer in range(n // 2): first, last = layer, n - layer - 1 for i in range(first, last): # save top top = matrix[l原创 2021-01-21 14:27:56 · 89 阅读 · 0 评论 -
字符串转url
def urlify_algo(string, length): """replace spaces with %20 and removes trailing spaces""" # convert to list because Python strings are immutable char_list = list(string) string = "" new_index = len(char_list) for i in reversed(ran原创 2021-01-21 14:19:21 · 1861 阅读 · 0 评论 -
检测排列组合
def check_permutation_by_sort(s1, s2): if len(s1) != len(s2): return False s1, s2 = sorted(s1), sorted(s2) for i in range(len(s1) - 1): if s1[i] != s2[i]: return False return Truedef check_permutation_by_count翻译 2021-01-21 14:18:00 · 113 阅读 · 0 评论 -
is_unique
def is_unique_chars_algorithmic(string): # Assuming character set is ASCII (128 characters) if len(string) > 128: return False # this is a pythonic and faster way to initialize an array with a fixed value. careful though # it wo翻译 2021-01-21 14:14:08 · 158 阅读 · 0 评论 -
三数之和
class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ if len(nums) < 3: return [] nums.sort() res = set() for i, v in enume原创 2021-01-18 19:19:44 · 80 阅读 · 0 评论 -
爬楼梯
# 直接递归解法,容易超时,python可以加个缓存装饰器,这样也算是将递归转换成迭代的形式了# 除了这种方式,还有增加步长来递归,变相的减少了重复计算# 还有一种方法,在递归的同时,用数组记忆之前得到的结果,也是减少重复计算class Solution: @functools.lru_cache(100) # 缓存装饰器 def climbStairs(self, n: int) -> int: if n == 1: return 1 if n原创 2021-01-18 19:08:05 · 71 阅读 · 0 评论 -
盛最多水的容器
class Solution: def maxArea(self, height: List[int]) -> int: l, r = 0, len(height) - 1 ans = 0 while l < r: area = min(height[l], height[r]) * (r - l) ans = max(ans, area) if height[l] &原创 2021-01-18 18:49:59 · 75 阅读 · 0 评论 -
移动零
class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ idxs = [idx for idx , num in enumerate(nums) if num == 0]原创 2021-01-18 15:59:38 · 71 阅读 · 0 评论 -
123另外的解
class Solution: def maxProfit(self, prices: List[int]) -> int: n = len(prices) if n == 0: return 0 minPri, maxPro1 = prices[0], 0 # 顺序遍历的最小pirce和最大利润 maxPri, maxPro2 = prices[-1],0 # 逆序遍历的最大price和最大利润原创 2021-01-09 22:50:43 · 90 阅读 · 0 评论 -
121
class Solution: def maxProfit(self, prices: List[int]) -> int: if len(prices) == 0: return 0 maxPro, minPrice = 0, prices[0] for i in range(1,len(prices)): if prices[i] <= minPrice: minPrice = prices[i]原创 2021-01-09 22:49:08 · 74 阅读 · 0 评论 -
123
class Solution(object): def maxProfit(self, prices): n = len(prices) buy1 = buy2 = -prices[0] sell1 = sell2 = 0 for i in range(1, n): buy1 = max(buy1, -prices[i]) sell1 = max(sell1, buy1 + prices[翻译 2021-01-09 22:18:37 · 66 阅读 · 0 评论 -
142
class Solution(object): def detectCycle(self, head): """ :type head: ListNode :rtype: ListNode """ hash_set = set() while head: if head in hash_set: return head has原创 2021-01-07 22:51:53 · 77 阅读 · 0 评论 -
46全排列
class Solution(object): def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ def backtrack(first = 0): # 所有数都填完了 if first == n: res.append(nums[原创 2021-01-07 22:49:32 · 105 阅读 · 0 评论 -
141 环形链表
class Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ if not head: return False p1 = p2 = head # p1每次跑1步,p2每次跑2步 while p2.next and p2.next翻译 2021-01-07 22:47:25 · 78 阅读 · 1 评论 -
打家劫舍系列
class Solution { public int rob(int[] nums) { int n=nums.length; int a_i=0, b_i=0; int dp_i=0; for(int i=n-1;i>=0;i--){ dp_i=Math.max(dp_i, nums[i]+b_i); b_i=a_i; a_i=dp_i; .原创 2021-01-04 22:17:36 · 108 阅读 · 1 评论 -
leetcode-1312
翻转一下 比对class Solution: def minInsertions(self, s: str) -> int: n = len(s) t = s[::-1] dp = [[0] * (n + 1) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, n + 1): dp[i][j翻译 2021-01-02 20:10:42 · 178 阅读 · 0 评论 -
leetcode-130
class Solution(object): def solve(self, board): """ :type board: List[List[str]] :rtype: void Do not return anything, modify board in-place instead. """ def fill(x, y): if x < 0 or x > m - 1 or原创 2021-01-02 20:05:39 · 114 阅读 · 1 评论 -
leetcode 1118 一月有多少天
class Solution: def numberOfDays(self, Y: int, M: int) -> int: D = [31,28,31,30,31,30,31,31,30,31,30,31] if Y % 400 == 0 or Y % 4 == 0 and Y % 100 != 0: D[1] += 1 return D[M - 1]原创 2021-01-01 22:08:39 · 104 阅读 · 0 评论 -
leetcode-1143最长公共子序列
class Solution(object): def longestCommonSubsequence(self, text1, text2): """ :type text1: str :type text2: str :rtype: int """ m, n = len(text1), len(text2) # 构建 DP table 和 base case dp翻译 2020-12-31 18:39:46 · 104 阅读 · 0 评论 -
leetcode-100
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): # @param p, a tree node # @param q, a tree node原创 2020-12-31 13:15:23 · 97 阅读 · 0 评论 -
leetcode-10
class Solution(object): def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ memo = {} def dp(i, j): if (i, j) not in memo: if j == len(p):原创 2020-12-31 12:51:18 · 100 阅读 · 0 评论 -
leetcode-3
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: right,ans,n,seen=-1,0,len(s),set() for i in range(n): if i: seen.remove(s[i-1]) while right+1<n and s[right+1] not in seen:翻译 2020-12-30 21:56:52 · 65 阅读 · 0 评论 -
剑指offer54
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def kthLargest(self, root, k): """ :type原创 2020-12-30 14:41:42 · 85 阅读 · 0 评论 -
leetcode239 滑动窗口最大值 python 双端队列
from collections import dequeclass Solution(object): def maxSlidingWindow(self, nums, k): """ :type nums: List[int] :type k: int :rtype: List[int] """ res = [] d = deque() for i in rang原创 2020-12-30 14:39:54 · 93 阅读 · 0 评论 -
leetcode20 合法的括号 栈 python版本
class Solution20: def isValid(self, s): stack = [] paren_map = { ')': '(', ']': '[', '}': '{' } for c in s: if c not in paren_map: stack.append(c)原创 2020-12-30 14:37:34 · 72 阅读 · 0 评论 -
leetcode25
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: # 翻转一个子链表,并且返回新的头与尾 def reverse(self, head: ListNode, tail: ListNode): pr原创 2020-12-30 14:29:16 · 66 阅读 · 0 评论 -
leetcode-242 有效的字母异位词 python版本
class Solution: def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ return set(s) == set(t) and all(s.count(i) == t.count(i) for i in set(s))原创 2020-12-30 14:02:43 · 87 阅读 · 0 评论 -
leetcode-53
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ n = len(nums) if n == 1: return nums[0] ans = pos = nums[0] for i in range(1, len原创 2020-12-29 21:16:25 · 86 阅读 · 0 评论 -
leetcode-3-无重复字符的最长子串
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ occ = set() n = len(s) # 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动 rk, ans = -1, 0 for i原创 2020-12-29 21:10:47 · 67 阅读 · 0 评论 -
leetcode-70爬楼梯
# 直接递归解法,容易超时,python可以加个缓存装饰器,这样也算是将递归转换成迭代的形式了# 除了这种方式,还有增加步长来递归,变相的减少了重复计算# 还有一种方法,在递归的同时,用数组记忆之前得到的结果,也是减少重复计算import functoolsclass Solution: @functools.lru_cache(100) # 缓存装饰器 def climbStairs(self, n: int) -> int: if n < 3:原创 2020-12-29 21:04:49 · 76 阅读 · 0 评论 -
斐波那契数列及其变种 装饰器版本和矩阵加速
from functools import reducefrom functools import wrapsdef memo(func): cache = {} @wraps(func) def wrap(*args): if args not in cache: cache[args] = func(*args) return cache[args] return wrap@memodef fib原创 2020-12-29 20:51:35 · 132 阅读 · 0 评论 -
迪克斯特拉算法
# -*- coding: utf-8 -*-# the graph 绘制图graph = {}graph["start"] = {}graph["start"]["a"] = 6graph["start"]["b"] = 2graph["a"] = {}graph["a"]["fin"] = 1graph["b"] = {}graph["b"]["a"] = 3gr...原创 2019-09-18 10:33:04 · 559 阅读 · 0 评论 -
快速排序
快速排序的应用原创 2019-08-12 10:56:02 · 105 阅读 · 0 评论 -
堆的应用
高性能定时器优先级队列合并有序小文件利用堆来求top K求中位数大文件的关键字的统计原创 2019-08-12 10:13:28 · 61 阅读 · 0 评论 -
堆排序
heap堆这种数据结构 应用的场景非常多最经典的莫过于堆排序了原地的 时间复杂度为 O(nlgn)的排序算法在实际的软件开发过程中 快速排序的性能要比堆排序好堆是一个完全二叉树每一个节点的值必须大于等于(或者小于等于)其子树中每个节点的值分为大顶堆 小顶堆数组存储堆排序数据访问方式没有快速排序友好对于同样的顺序 在排序过程中 堆排序的数据交换次数多于快速排序...原创 2019-08-12 09:36:32 · 70 阅读 · 0 评论