题目
代码
执行用时:92 ms, 在所有 Python3 提交中击败了39.44% 的用户
内存消耗:15.2 MB, 在所有 Python3 提交中击败了66.90% 的用户
通过测试用例:97 / 97
class Solution:
def minPartitions(self, n: str) -> int:
cnt=Counter(str(n))
return int(max(cnt))
【方法2】
执行用时:432 ms, 在所有 Python3 提交中击败了5.63% 的用户
内存消耗:15.2 MB, 在所有 Python3 提交中击败了64.79% 的用户
通过测试用例:97 / 97
class Solution:
def minPartitions(self, n: str) -> int:
ans=0
right=len(n)-1
while right>=0:
ans=max(ans,int(n[right]))
right-=1
return ans

本文对比了两种Python解决方案,一种在寻找最大字符计数上表现优异,执行时间为92ms,内存消耗15.2MB;另一种虽然稍慢但内存效率更高,适用于对内存敏感场景。详细解析了如何在`minPartitions`函数中分别使用Counter和迭代法求解字符串最小分割问题。
411

被折叠的 条评论
为什么被折叠?



