随笔分类 - leetcode
摘要:题目描述: 第一次提交: class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-
阅读全文
摘要:题目描述: 第一次提交: class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: if head==None or head.next==None: return head head.next = self.de
阅读全文
摘要:题目描述: 第一次提交:(超时) class Solution: def climbStairs(self, n: int) -> int: if n == 0 or n == 1 or n == 2: return n return self.climbStairs(n - 1) + self.c
阅读全文
摘要:题目描述: 第一次提交:(会超时) class Solution: def mySqrt(self, x: int) -> int: if x==0 or x==1: return x for i in range(1,x): if i*i<=x and (i+1)**2>x: return i V
阅读全文
摘要:题目描述: 第一次提交: class Solution: def addBinary(self, a: str, b: str) -> str: list_a,list_b=[],[] for s in a: list_a.append(int(s)) for s in b: list_b.appe
阅读全文
摘要:题目描述: 第一次提交: class Solution: def plusOne(self, digits): digits[-1]=digits[-1]+1 for i in range(len(digits)-1,0,-1): if digits[i]==10: digits[i]=0 digi
阅读全文
摘要:题目描述: 第一次解答: class Solution: def lengthOfLastWord(self, s: str) -> int: L=s.strip().split(" ") if L[-1]=="" : return 0 return len(L[-1]) 优化后: class So
阅读全文
摘要:题目描述: 方法一:O(N) class Solution(object): def maxSubArray(self, nums): sum = 0 max_sub_sum = nums[0] for num in nums: sum += num if sum > max_sub_sum: ma
阅读全文
摘要:恢复内容开始 题目描述: 第一次提交: class Solution: def countAndSay(self, n: int) -> str: f = "1" for i in range(n-1): count = 0 c = '' for j in range(len(f)): if j =
阅读全文
摘要:题目: 第一次提交; class Solution: def searchInsert(self, nums: List[int], target: int) -> int: for i in range(len(nums)): if nums[i] >= target : return i if
阅读全文
摘要:题目: 第一次提交: class Solution: def strStr(self, haystack: str, needle: str) -> int: if not len(needle): return 0 for i in range(len(haystack)): if i+len(n
阅读全文
摘要:题目: 第一次提交: class Solution: def removeElement(self, nums, val: int) -> int: for i in range(len(nums)-1,-1, -1):#此处中间为range(,中间值为-1,) if nums[i] == val:
阅读全文
摘要:题目描述: 第一次提交: class Solution: def removeDuplicates(self, nums) -> int: for i in range(len(nums)-1,0,-1):#注意要倒序** if nums[i]==nums[i-1]: del(nums[i]) re
阅读全文
摘要:题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def merge
阅读全文
摘要:python字符串与列表的相互转换 学习内容: 1.字符串转列表 2.列表转字符串 1. 字符串转列表 str1 = "hi hello world" print(str1.split(" "))输出:['hi', 'hello', 'world'] 2. 列表转字符串 l = ["hi","hel
阅读全文
摘要:笔记: python if not 判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法。 使用if not x这种写法的前提是:必须清楚x等于None, Fa
阅读全文
摘要:错误记录 class Solution: def romanToInt(self, s: str) -> int: d = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} r=0 for i in range(len(s)): if d[s[
阅读全文
摘要:题目描述 方法一:转换为字符串 class Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False else: y=str(x)[::-1] return y==str(x) 方法二;数字反转 class Solu
阅读全文
摘要:Python join()方法 join()方法语法: str.join(sequence) 参数 sequence -- 要连接的元素序列。 返回值 返回通过指定字符连接序列中元素后生成的新字符串。 实例 #!/usr/bin/python # -*- coding: UTF-8 -*- str
阅读全文
浙公网安备 33010602011771号