当前位置: 首页> 教育> 高考 > 茂名企业自助建站_品牌建设再发力_视频网站搭建_搜百度盘

茂名企业自助建站_品牌建设再发力_视频网站搭建_搜百度盘

时间:2025/8/27 4:35:57来源:https://blog.csdn.net/m0_73030013/article/details/146181792 浏览次数:0次
茂名企业自助建站_品牌建设再发力_视频网站搭建_搜百度盘

leetcode刷题:

1. 334. 递增的三元子序列 - 力扣(LeetCode)

方法一:使用贪心算法求解
class Solution(object):def increasingTriplet(self, nums):first = nums[0]second = float('inf')for i in nums:if i>second:return Trueelif i>first:second=ielif i<first:first=ireturn False

这种方法通过不断地贪心来解决问题。首先解决的是最大值,如果达到a<b<c那么就直接true,其次是次大,如果比最大值小比最小值大,那么就是次大,最后如果比最小值还小,说明成就递增三元子序列的可能更大一点。

#方法二:用数组来维护
class Solution(object):def increasingTriplet(self, nums):n=len(nums)left,right=[float("inf")]*n,[float("-inf")]*n# 维护两个数组,left表示不包括此时左侧的最小值,right表示不包括此时的右侧的最大值for i in range(1,n):left[i]=min(left[i-1],nums[i-1])for i in range(n-2,0,-1):right[i]=max(right[i+1],nums[i+1])for i in range(1, n - 1):if left[i] < nums[i] < right[i]:return Truereturn False

这种方法维护了两个数组,分别是left和right,其中left表示当前数字左边的比当前数字小的,right表示当前数字右边的比当前数字大的,然后再比较left<nums<right,如果满足这个条件,说明存在递增子序列,如果不满足,则证明不存在,返回False

2.1657. 确定两个字符串是否接近 - 力扣(LeetCode)

class Solution:def closeStrings(self, word1: str, word2: str) -> bool:dict1=Counter(word1)dict2=Counter(word2)return dict1.keys()==dict2.keys() and sorted(dict1.values())==sorted(dict2.values())

首先比较字符出现是否一样,然后比较字符出现的次数是否一样。

plus:当不能替换的时候,就直接

class Solution:def closeStrings(self, word1: str, word2: str) -> bool:dict1=Counter(word1)dict2=Counter(word2)return dict1==dict2

3.437. 路径总和 III - 力扣(LeetCode)

class Solution:def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:if root is None:return 0def dfs(root,target):count=0if root.val==target:count+=1if root.left:count+=dfs(root.left,target-root.val)if root.right:count+=dfs(root.right,target-root.val)return countreturn dfs(root,targetSum)+self.pathSum(root.left,targetSum)+self.pathSum(root.right,targetSum)

这道题难点在于是否考虑本节点被选择,以及dfs的运用。

关键字:茂名企业自助建站_品牌建设再发力_视频网站搭建_搜百度盘

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: