![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
胡侃有料
流水不争先,争的是滔滔不绝
展开
-
【LeetCode】【refine 3】题号:*3. 无重复字符的最长子串
every blog every motto: You will never know unless you try0. 前言改变策略1. 正文1.1 题目1.2 代码class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if not s: return 0 left = 0 looked = set() # 窗口 n = len原创 2021-08-22 22:12:14 · 84 阅读 · 0 评论 -
【LeetCode】【refine 2】题号:*2. 两数相加
every blog every motto: You will never know unless you try0. 前言冲冲冲1. 正文1.1 题目1.2 代码# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution:原创 2021-08-22 20:24:02 · 131 阅读 · 0 评论 -
【LeetCode】【refine 1】题号:1. 两数之和
every blog every motto: You will never know unless you try0. 前言改变策略1. 正文1.1 题目1.2 代码class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: com_list = [target - i for i in nums] # print(com_list)原创 2021-08-21 21:50:30 · 92 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*481. 神奇字符串
every blog every motto: You will never know unless you try0. 前言冲冲冲1. 字符串1.1 题目1.2 代码class Solution: def magicalString(self, n: int) -> int: # 初始情况 if n == 0: return 0 elif n <= 3: return 1原创 2021-08-21 20:34:25 · 79 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*165. 比较版本号
every blog every motto: You will never know unless you try0. 前言冲冲冲1. 字符串1.1 题目1.2 代码class Solution: def compareVersion(self, version1: str, version2: str) -> int: ver1_list = version1.split('.') ver2_list = version2.split('.原创 2021-08-19 22:23:53 · 66 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*12. 整数转罗马数字
every blog every motto: You will never know unless you try0. 前言冲冲冲1. 字符串1.1 题目1.2 代码class Solution: def intToRoman(self, num: int) -> str: val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] strs = ['M', 'CM', 'D', '原创 2021-08-19 21:25:59 · 65 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*13. 罗马数字转整数
every blog every motto: You will never know unless you try0. 前言冲冲冲1. 字符串1.1 题目1.2 代码class Solution: def romanToInt(self, s: str) -> int: pattern = { 'IV': 4, 'IX': 9, 'XL': 40, 'XC': 90,原创 2021-08-15 21:40:24 · 76 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*8. 字符串转换整数 (atoi)
every blog every motto: You will never know unless you try0. 前言冲冲冲1. 字符串1.1 题目1.2 代码class Solution: def myAtoi(self, s: str) -> int: s = s.strip() # 去除前后空字符 result = '' flag = 1 INT_MAX = 2 ** 31 - 1原创 2021-08-15 20:56:46 · 68 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*443. 压缩字符串
every blog every motto: You will never know unless you try0. 前言冲冲冲1. 字符串1.1 题目1.2 代码class Solution: def compress(self, chars: List[str]) -> int: anchor = 0 # 字符的起始位置 writer = 0 # 记录该写位置的索引 for current_read_index, c原创 2021-08-11 21:12:34 · 128 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*38. 外观数列
every blog every motto: You will never know unless you try0. 前言冲冲冲1. 字符串1.1 题目1.2 代码class Solution: def countAndSay(self, n: int) -> str: """迭代""" strs = '1' # 第一个字符 # 余下的n-1 for i in range(n - 1):原创 2021-08-08 22:31:30 · 76 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*640. 求解方程
every blog every motto: You will never know unless you try0. 前言冲冲冲1. 字符串1.1 题目1.2class Solution: def solveEquation(self, equation: str) -> str: def num_x(eq): nums = 0 # 左边数字的和 x_nums = 0 # 左边x数字的和原创 2021-08-05 09:11:20 · 106 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*537. 复数乘法
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def complexNumberMultiply(self, num1: str, num2: str) -> str: s1, v1 = num1.split('+') s2, v2 = num2.split('+')原创 2021-08-04 21:39:29 · 77 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*151. 翻转字符串里的单词
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def optimalDivision(self, nums: List[int]) -> str: lens = len(nums) if lens == 0: return '' elif le原创 2021-08-04 21:23:07 · 81 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*539. 最小时间差
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def findMinDifference(self, timePoints: List[str]) -> int: ms = [] for t in timePoints: h, m = t.split(':')原创 2021-08-04 20:55:40 · 105 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*506. 相对名次
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def findRelativeRanks(self, score: List[int]) -> List[str]: score_sort = sorted(score, reverse=True) # 排名 rank =原创 2021-08-04 20:24:18 · 74 阅读 · 0 评论 -
【LeetCode】【字符串】题号:412. Fizz Buzz
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def fizzBuzz(self, n: int) -> List[str]: result = [] for ele in range(1,n+1): if ele % 3 == 0 and e原创 2021-08-04 15:58:48 · 73 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*299. 猜数字游戏
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def getHint(self, secret: str, guess: str) -> str: # 方法一: bulls = 0 # 公牛 cows = 0 # 奶牛 bulls_cows = 0原创 2021-08-03 22:43:46 · 111 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*551. 学生出勤记录 I
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def checkRecord(self, s: str) -> bool: count = {'A':0,'L':0,'P':0} p_flag = 0 index = 0 for ele in s:原创 2021-08-02 22:59:37 · 60 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*657. 机器人能否返回原点
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def judgeCircle(self, moves: str) -> bool: # 方法一 count = collections.Counter(moves) if count['U'] == count['D']原创 2021-08-02 22:27:23 · 55 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*423. 从英文中重建数字
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def originalDigits(self, s: str) -> str: # 统计各字符出现的数量 count = collections.Counter(s) # print(count) out原创 2021-08-02 22:13:50 · 57 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*451. 根据字符出现频率排序
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def frequencySort(self, s: str) -> str: result = '' count = collections.Counter(s) for i,j in count.most_common原创 2021-08-02 11:17:27 · 74 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*49. 字母异位词分组
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: # 字典 map = collections.defaultdict(list) for s i原创 2021-08-02 10:06:19 · 59 阅读 · 0 评论 -
【LeetCode】【字符串】题号:242. 有效的字母异位词
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2两种方法哈希数组class Solution: def isAnagram(self, s: str, t: str) -> bool: # 方法一:哈希,统计字符 # s_freq = collections.Counter(s)原创 2021-08-01 21:13:31 · 74 阅读 · 0 评论 -
【LeetCode】【字符串】题号:383. 赎金信
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: ran_freq = collections.Counter(ransomNote) mag_freq = c原创 2021-08-01 20:51:53 · 73 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*389. 找不同
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def findTheDifference(self, s: str, t: str) -> str: s_freq = collections.Counter(s) t_freq = collections.Counter(t)原创 2021-08-01 20:27:10 · 70 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*387. 字符串中的第一个唯一字符
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def firstUniqChar(self, s: str) -> int: freq = collections.Counter(s) for index,ele in enumerate(s): if f原创 2021-08-01 20:10:28 · 61 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*151. 翻转字符串里的单词
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def reverseWords(self, s: str) -> str: s = s.strip() temp = s.split(" ") # 根据空格拆分 # 去除单词中间的null原创 2021-07-28 15:03:34 · 62 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*557. 反转字符串中的单词 III
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def reverseWords(self, s: str) -> str: def reverse(s_list): left, right = 0, len(s_list) - 1 while lef原创 2021-07-28 14:01:13 · 73 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*541. 反转字符串 II
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def reverseStr(self, s: str, k: int) -> str: s = list(s) # 如果总字符小于k个 if len(s) < k: s = self.rev原创 2021-07-26 23:08:49 · 70 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*344. 反转字符串
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """原创 2021-07-26 21:54:37 · 74 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*58. 最后一个单词的长度
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def lengthOfLastWord(self, s: str) -> int: if len(s) == 0: return 9 count = 0 # 单词长度 for i in s[:原创 2021-07-26 21:08:11 · 64 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*434. 字符串中的单词数
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def countSegments(self, s: str) -> int: # 统计开始字母的个数 count = 0 # 字符数 for i in range(len(s)): if (原创 2021-07-26 20:28:04 · 58 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*14. 最长公共前缀
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if len(strs)<1: return "" length = len(strs[0])原创 2021-07-24 22:14:57 · 81 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*125. 验证回文串
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2 题解class Solution: def isPalindrome(self, s: str) -> bool: if len(s)<=1 : return True else: left = 0原创 2021-07-22 22:31:22 · 75 阅读 · 0 评论 -
【LeetCode】【字符串】题号:*520. 检测大写字母
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 字符串1.1 题目1.2class Solution: def detectCapitalUse(self, word: str) -> bool: return word.islower() or word.isupper() or word.istitle()1.3...原创 2021-07-22 21:28:11 · 74 阅读 · 0 评论 -
【LeetCode】【数组】题号:*4238. 除自身以外数组的乘积
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 正文1.2class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: # 先算左侧乘积 answer = [1]*len(nums) for i in range(1,len(nums)):原创 2021-07-22 21:13:34 · 60 阅读 · 0 评论 -
【LeetCode】【数组】题号:*304,二维区域和检索
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 正文1.2 题解class NumMatrix: def __init__(self, matrix: List[List[int]]): # 前缀和 m = len(matrix) # 行 n = len(matrix[0]) # 列 sum = [[0]*(n+1原创 2021-07-21 22:51:09 · 80 阅读 · 0 评论 -
【LeetCode】【数组】题号:*303,区域和检索
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 正文1.2 题解class NumArray: def __init__(self, nums: List[int]): self.nums = nums def sumRange(self, left: int, right: int) -> int: if lef原创 2021-07-21 21:53:23 · 58 阅读 · 0 评论 -
【LeetCode】【数组】题号:*289,生命游戏
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 正文1.2 题解class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """原创 2021-07-20 21:47:03 · 64 阅读 · 0 评论 -
【LeetCode】【数组】题号:73,矩阵置零
every blog every motto: You will never know unless you try0. 前言生活好难,再坚持坚持!1. 正文1.2 题解class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """原创 2021-07-18 11:16:47 · 65 阅读 · 0 评论