欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Leetcode 3228. Maximum Number of Operations to Move Ones to the End

Leetcode 3228. Maximum Number of Operations to Move Ones to the End

2024/10/24 5:24:09 来源:https://blog.csdn.net/codename_cys/article/details/140592795  浏览:    关键词:Leetcode 3228. Maximum Number of Operations to Move Ones to the End
  • Leetcode 3228. Maximum Number of Operations to Move Ones to the End
    • 1. 解题思路
    • 2. 代码实现
  • 题目链接:3228. Maximum Number of Operations to Move Ones to the End

1. 解题思路

这一题不难分析得到,要获得最多的操作次数,只需要从左往右依次执行即可,此时,每一次遇到一个10结构,能够执行的操作次数就是其左侧所有的1的个数。

我们将其翻译为代码语言即可。

2. 代码实现

给出python代码实现如下:

class Solution:def maxOperations(self, s: str) -> int:pre = 0ans = 0for i, ch in enumerate(s):if ch == "1":pre += 1elif i > 0 and s[i-1] == "1":ans += prereturn ans

提交代码评测得到:耗时91ms,占用内存17.3MB。

版权声明:

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

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