自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(20)
  • 收藏
  • 关注

原创 LeetCode 11 Container With Most Water Writeup

盛最多水的容器比官方40ms内存和用时却都是5%,搞不懂剩下95%是怎么做的class Solution: def maxArea(self, height: List[int]) -> int: i, j = 0, len(height)-1 maxium = 0 while i < j: temp = min(height[i],height[j]) * (j - i) if max

2021-03-06 20:32:58 93

原创 LeetCode 剑指 Offer 54 二叉搜索树的第k大节点 Writeup

二叉搜索树的第k大节点二叉树学的不太好,只想到一种容易理解的方法题解里排第一那样中序遍历时对k递减会好很多class Solution: def kthLargest(self, root: TreeNode, k: int) -> int: def nodeNums(root): if not root: return 0 return nodeNums(root.left) + nodeN

2021-03-02 17:02:36 88

原创 LeetCode 剑指 Offer 63 Writeup

股票的最大利润class Solution: def maxProfit(self, prices: List[int]) -> int: days = len(prices) if days == 0: return 0 dp = [0] * days for i in range(1, days): dp[i] = prices[i] - min(pric

2021-02-28 19:41:05 113

原创 LeetCode 5689 Weekly Contest 230.1 Writeup

统计匹配检索规则的物品数量class Solution: def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int: if ruleKey == "type": col = 0 elif ruleKey == "color": col = 1 else: col =

2021-02-28 11:06:22 131

原创 LeetCode 120 Triangle Writeup

三角形最小路径和两种解决边界问题的方案解法一class Solution: def minimumTotal(self, triangle: List[List[int]]) -> int: rownum = len(triangle) dp = [[float("inf") for col in range(rownum + 1)] for row in range(rownum)] for row in range(rownum):

2021-02-28 10:51:54 93

原创 LeetCode 1267 Count Servers That Communicate Writeup

统计参与通信的服务器解题思路用服务器总数减去不参与通信的服务器数量(比官方题解快了24ms)class Solution: def countServers(self, grid: List[List[int]]) -> int: rowlenth = len(grid) noConnect = 0 # 记录可能存在未参与通信的服务器所在的行数 suspect = [0] * rowlenth #找到可能

2021-02-27 20:04:24 55

原创 LeetCode 746 Min Cost Climbing Stairs Writeup

使用最小花费爬楼梯class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: dp = [0] * (len(cost) + 1) for i in range(2,len(cost) + 1): dp[i] = min(dp[i - 1] + cost[i - 1],dp[i - 2] + cost[i - 2]) return

2021-02-25 19:34:43 76

原创 LeetCode 1 Two Sum Writeup

两数之和#比官方题解还快class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i, elment in enumerate(nums): if (target - elment) in nums[(i + 1):]: return [i, nums[(i + 1):].index(target - elment)

2021-02-25 16:20:01 63

原创 LeetCode 70 Climbing Stairs Writeup

爬楼梯class Solution: def climbStairs(self, n: int) -> int: if n == 1: return 1 dp = [0 for _ in range(n + 1)] dp[1], dp[2] = 1, 2 for i in range(3, (n + 1)): dp[i] = dp[i - 1] + dp[i - 2]

2021-02-25 16:11:06 56

原创 LeetCode 119 Pascals Triangle II Writeup

杨辉三角 IIclass Solution: def getRow(self, rowIndex: int) -> List[int]: if rowIndex == 0: return [1] elif rowIndex == 1: return [1,1] else: triangle = [1,1] while len(triangle) &l

2021-02-22 16:18:44 68

原创 LeetCode 1534 Count Good Triplets Writeup

统计好三元组解法一使用了剪枝法,比完全暴力法稍好一些class Solution: def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int: count = 0 i = 0 while i+2 < len(arr): j = i + 1 while j+1 < len(arr):

2021-02-15 15:42:13 59

原创 LeetCode 1535 Find The Winner Of An Array Game Writeup

找出数组游戏的赢家解法一class Solution(object): def getWinner(self, arr, k): """ :type arr: List[int] :type k: int :rtype: int """ roundnum = 0 winnum = 0 while winnum < k and roundnum < len(a

2021-02-12 18:19:34 61

原创 LeetCode 832 Flipping An Image Writeup

翻转图像原题class Solution: def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]: row = 0 while row < len(A): column = 0 while column < len(A)//2: A[row][column],A[row][len(A)-

2021-02-09 22:27:17 52

原创 LeetCode 1550 Three Consecutive Odds Writeup

存在连续三个奇数的数组原题解法一class Solution: def threeConsecutiveOdds(self, arr: List[int]) -> bool: pos = 0 while pos + 2 < len(arr): if arr[pos] & 1 == 1 and arr[pos + 1] & 1 == 1 and arr[pos + 2] & 1 == 1:

2021-01-28 12:30:23 89

原创 LeetCode 1588 Sum Of All Odd Length Subarrays Writeup

所有奇数长度子数组的和原题from typing import Listclass Solution: def sumOddLengthSubarrays(self, arr: List[int]) -> int: sums = 0 for i in range(1,len(arr)+1,2): #步长从1到arr长度加一,左闭右开 j = 0 while i+j <= len(arr):

2021-01-27 15:46:59 102

原创 LeetCode 268 Missing Number

丢失的数字原题class Solution: def missingNumber(self, nums: List[int]) -> int: nums.sort() pos = 0 while pos != len(nums): if pos != nums[pos]: return pos else: pos +=1

2021-01-25 21:21:28 60

原创 LeetCode 1441 Build An Array With Stack Operations

用栈操作构建数组原题class Solution: def buildArray(self, target, n) : goal = [] pos = 0 for i in range(1,n+1): goal.append("Push") if target[pos] != i: goal.append("Pop") else:

2021-01-24 19:53:59 81

原创 LeetCode 496 Next Greater Element I

LeetCode 496 Next Greater Element I原题class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: result = [-1]*len(nums1) #存放输出结果 for i in nums1: for j in nums2[nums2.index(i)+1:]:

2021-01-23 09:51:14 71

原创 POJ 1006 Biorhythms Writeup

BiorhythmsTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 143601 Accepted: 46190DescriptionSome people believe that there are three cycles in a person's life that start the day he or she is ...

2018-04-11 17:44:11 156 1

原创 POJ 1004 Financial Management Writeup

萌新试水,求平均数的水题,大佬请忽略。。                                               Financial ManagementTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 109258 Accepted: 47427DescriptionLarry graduated this y...

2018-04-08 20:49:40 187

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除