随笔分类 - leetcode
leetcode刷题记录
摘要:看着挺简单,结果不断出错不断修改,就很容易绕晕了,用了一个小时四十分钟左右才完成。。。好慢哦: class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ a=str.lstrip() if le
阅读全文
摘要:class Solution(object): def convert(self, s, numRows): """ :type s: str :type numRows: int :rtype: str """ if numRows==1: return s if len(s)<2: return
阅读全文
摘要:class Solution: def isPalindrome(self, s: str) -> bool: import re from string import punctuation add_punc="' '" all_punc=add_punc+punctuation b=''.joi
阅读全文
摘要:class Solution: def searchRange(self, nums: List[int], target: int) -> List[int]: a=nums.count(target) if a==0: return [-1,-1] else: d=nums.index(targ
阅读全文
摘要:——2019.10.8 我太蠢了,当时并不知道什么是logn级别,就暴力瞎写,啥也不懂,十分尴尬。 二分法,确定边界 public int search(int[] nums, int target) { //未知位置发生旋转,如何确定是哪个位置发生的? //二分法如何使用,如何判断边界? int
阅读全文
摘要:自己做出来效果并不是很好: class Solution: def removeDuplicates(self, nums) -> int: i=0 while i<len(nums)-2: j,k=i+1,i+2 if nums[i]==nums[j]: if nums[j]==nums[k]:
阅读全文
摘要:双指针: class Solution: def removeDuplicates(self, nums) -> int: if len(nums)<2: return len(nums) i,j=0,1 while j<len(nums): if nums[i]==nums[j]: nums.po
阅读全文
摘要:我自己做了一上午还是有错误,参考了别人的,一目了然。 双指针!!! class Solution: def fourSum(self, nums, target: int): n = len(nums) if n < 4: return [] nums.sort() res = [] for i i
阅读全文
摘要:class Solution: def trap(self, height) -> int: if len(height)<2: return 0 H=max(height) I=height.index(H) S=H*len(height) #print(S) S_1=sum(height) #p
阅读全文
摘要:双指针法: class Solution: def maxArea(self, height) -> int: if len(height)<=1: return 0 #双指针法 i,j,S=0,len(height)-1,0 while i<j: if height[i]<height[j]: S
阅读全文
摘要:我自己写的超时了。。。。 class Solution: def threeSum(self, nums) : nums.sort() #print(nums) nums1=[] s=[] if nums.count(0)>=3: s.append([0,0,0]) for i in range(l
阅读全文
摘要:class Solution: def findMedianSortedArrays(self, nums1, nums2) -> float: c=sorted(nums1+nums2) if len(c)%2==0: return (c[len(c)//2]+c[len(c)//2-1])/2
阅读全文
摘要:class Solution: def singleNumber(self, nums) -> int: k={} i=0 while i < len(nums): m=nums[i] if m in k.keys(): k[m]=2 i+=1 else: k[m]=1 i+=1 for i,j i
阅读全文
摘要:class Solution: def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ #print(len(matrix))#行数 #p
阅读全文
摘要:1 class Solution: 2 def climbStairs(self, n: int) -> int: 3 #假设爬了x次一个台阶,y个两次台阶 4 #求x与y的组合序列 5 if n==0: 6 return 0 7 m=0 8 for x in range(n+1): 9 #prin
阅读全文
摘要:超出时间限制。。 1 class Solution: 2 def mySqrt(self, x: int) -> int: 3 for i in range(0,x//2+2): 4 if x>=i**2 and x<(i+1)**2: 5 return i 好气哦。。。加油想想怎么改进。。。 修改
阅读全文
摘要:class Solution: def isSameTree(self, p, q) -> bool: if not p and not q: return True elif p is not None and q is not None: if p.val==q.val: return self
阅读全文
摘要:1 class Solution: 2 def searchInsert(self, nums, target: int) -> int: 3 if target in nums: 4 return nums.index(target) 5 else: 6 if target>=nums[-1]:
阅读全文
摘要:早上两个小时才写出来,但是运行超时。。。 感觉自己是真的不会算法,应该系统学习一下才对。 代码我先粘在这里,然后慢慢优化: 要加油哦,学算法!!!!! ——2019.9.20 之前的惨不忍睹,已删。 方法一,暴力法,重点就是判断一个字符串是否是回文子串,以及设置两个参数来返回最后结果, 即,只需要记
阅读全文
摘要:用时8分20秒: public int maxProfit(int[] prices) { int n = prices.length; if(n<=1) return 0; int min_price = prices[0]; int interest = 0; for(int i = 1;i<n
阅读全文

浙公网安备 33010602011771号