随笔分类 -  LeetCode

摘要:LeetCode168:168. Excel表列名称 - 力扣(LeetCode) (leetcode-cn.com) 看了题解,计算除以26的商和余数,利用ASCII码从最后一位依次往前计算。 目前还有一点不明白,第5行。 1 class Solution: 2 def convertToTitl 阅读全文
posted @ 2021-02-24 21:10 vv_869 阅读(255) 评论(0) 推荐(0)
摘要:LeetCode136:https://leetcode-cn.com/problems/single-number/submissions/ 解题思路:参考题解,利用异或 。即将所有数字异或,然后相同的数字消除为0,剩下的即为所求。 1 class Solution: 2 def singleNu 阅读全文
posted @ 2021-02-20 17:07 vv_869 阅读(95) 评论(0) 推荐(0)
摘要:LeetCode119:https://leetcode-cn.com/problems/pascals-triangle-ii/submissions/ 解题思路:在原来LeetCode118的基础上稍微修改了一下。 1 class Solution: 2 def getRow(self, row 阅读全文
posted @ 2021-02-19 17:38 vv_869 阅读(116) 评论(0) 推荐(0)
摘要:LeetCode67:https://leetcode-cn.com/problems/add-binary/submissions/ 思路:利用内置函数 1 class Solution: 2 def addBinary(self, a: str, b: str) -> str: 3 return 阅读全文
posted @ 2021-02-19 16:45 vv_869 阅读(80) 评论(0) 推荐(0)
摘要:LeetCode58:https://leetcode-cn.com/problems/length-of-last-word/ 解题思路:利用字符串的内置函数 1 class Solution: 2 def lengthOfLastWord(self, s: str) -> int: 3 retu 阅读全文
posted @ 2021-02-19 11:53 vv_869 阅读(92) 评论(0) 推荐(0)
摘要:LeetCode35:https://leetcode-cn.com/problems/search-insert-position/ 解题思路:一种比较笨的方法,直接比较 # 先判断数字是否在给定的数组中 # 若在:遍历数组,找到该元素,并返回下标 # 若不在:遍历数组,比较大小,返回插入位置的索 阅读全文
posted @ 2021-02-18 18:41 vv_869 阅读(77) 评论(0) 推荐(0)
摘要:LeetCode28:https://leetcode-cn.com/problems/implement-strstr/submissions/ 解题思路:滑动窗法 1 class Solution: 2 def strStr(self, haystack: str, needle: str) - 阅读全文
posted @ 2021-02-18 16:52 vv_869 阅读(75) 评论(0) 推荐(0)
摘要:LeetCode27:27. 移除元素 - 力扣(LeetCode) (leetcode-cn.com) # 1 先对列表中需要移除的元素计数 # 2 移除该元素 1 class Solution: 2 def removeElement(self, nums: List[int], val: in 阅读全文
posted @ 2021-01-31 14:52 vv_869 阅读(85) 评论(0) 推荐(0)
摘要:LeetCode26 删除排序数组中的重复项 # 删除重复元素,解题方法:双指针 # 1 定义2个指针,慢指针i,从0开始;快指针j,从1开始 # 2 移动指针,如果当前nums[i] = nums[j],则j加1,i不动;如果不相等,则i,j均加1 1 class Solution: 2 def 阅读全文
posted @ 2021-01-30 22:45 vv_869 阅读(125) 评论(0) 推荐(0)
摘要:LeetCode14 最长公共前缀 链接:14. 最长公共前缀 - 力扣(LeetCode) (leetcode-cn.com) 解题思路: # 解题思路:把字符串数组的比较转换为2个字符串的比较 # 1通过比较字符串大小,确定最大值和最小值 # 2根据最小值的长度进行遍历,按位比较最大值和最小值, 阅读全文
posted @ 2021-01-26 22:20 vv_869 阅读(114) 评论(0) 推荐(0)
摘要:LeetCode13 罗马数字转整数 题目链接:https://leetcode-cn.com/problems/roman-to-integer/ 解题思路: # 1把罗马数字和对应的数值定义为字典; # 2计算输入字符的长度; # 3判断左边的数值是否小于右边,如果小于,则在总值中减去;反之加上 阅读全文
posted @ 2021-01-24 22:37 vv_869 阅读(124) 评论(0) 推荐(0)
摘要:LeetCode9 回文数 题目:https://leetcode-cn.com/problems/palindrome-number/ 解题思路如下: # 判断一个整数是否是回文数。 # 回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 # 1先判断,若为负,返回false; # 2 阅读全文
posted @ 2021-01-24 00:00 vv_869 阅读(107) 评论(0) 推荐(0)
摘要:LeetCode7 整数反转 题目:https://leetcode-cn.com/problems/reverse-integer/ 解题思路如下: 1. 先判断x是否为0,若为0,则直接返回0. 2. 判断x的正负,若为负,则绝对值处理,转换为正数。此处采用标识符flag应对正负的情况。 3. 阅读全文
posted @ 2021-01-22 23:42 vv_869 阅读(211) 评论(0) 推荐(0)
摘要:1. 题目描述 注: 回溯法 2. 代码 1 class Solution: 2 def generateParenthesis(self, n: int) -> List[str]: 3 y = list() 4 string = '(' 5 L = 1 6 R = 0 7 self.BackTr 阅读全文
posted @ 2020-10-25 10:28 vv_869 阅读(108) 评论(0) 推荐(0)
摘要:1. 题目描述 注: 属于动态规划, 保存已解决的子问题的答案,在需要时再找出已求得的答案, 这样就可以避免大量的重复计算, 节省时间. 可以用一个表来记录所有已解的子问题的答案, 不管该子问题以后是否被用到, 只要它被计算过, 就将其结果填入表中. 2. 代码 1 class Solution: 阅读全文
posted @ 2020-10-25 09:57 vv_869 阅读(104) 评论(0) 推荐(0)
摘要:1. 题目描述 2. 代码 1 import itertools 2 class Solution: 3 def permute(self, nums: List[int]) -> List[List[int]]: 4 n = len(nums) 5 temp = itertools.permuta 阅读全文
posted @ 2020-10-23 21:32 vv_869 阅读(77) 评论(0) 推荐(0)
摘要:1. 题目描述 注: 动态规划 2. 代码 1 import sys 2 class Solution: 3 def maxProfit(self, prices: 'List[int]') -> int: 4 n = len(prices) 5 maxval = 0 6 minval = sys. 阅读全文
posted @ 2020-10-23 20:00 vv_869 阅读(63) 评论(0) 推荐(0)
摘要:1. 题目描述 注: 动态规划 2. 代码 1 class Solution: 2 def climbStairs(self, n: int) -> int: 3 if n <= 2: 4 return n 5 else: 6 dp = [0] * (n+1) 7 dp[1] = 1 8 dp[2] 阅读全文
posted @ 2020-10-21 21:17 vv_869 阅读(76) 评论(0) 推荐(0)
摘要:1. 题目描述 注: 属于动态规划. 2. 代码 1 class Solution: 2 def maxSubArray(self, nums: List[int]) -> int: 3 for i in range(1,len(nums)): 4 nums[i] = max(nums[i-1] + 阅读全文
posted @ 2020-10-21 20:45 vv_869 阅读(75) 评论(0) 推荐(0)
摘要:1. 题目描述 2. 代码 1 class Solution: 2 def isPowerOfThree(self, n: int) -> bool: 3 while n > 2 and n % 3 == 0: 4 n = n // 3 5 return n == 1 阅读全文
posted @ 2020-10-20 20:51 vv_869 阅读(62) 评论(0) 推荐(0)