Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
给出一个0-1字符串,问最少操作几次可以将字符串变为....0-1-0-1....用两个计数器记录首位取0和取1分别要操作几次,取两者较小值


#1758
#
Runtime: 25 ms (Beats 79.41%)
#
Memory: 13.4 MB (Beats 88.24)

class Solution(object):
    def minOperations(self, s):
        """
        :type s: str
        :rtype: int
        
"""
        ans1, ans2 = 0, 0
        for i in xrange(0, len(s)):
            if i % 2:
                if s[i] == '0':
                    ans1 += 1
                else:
                    ans2 += 1
            else:
                if s[i] == '1':
                    ans1 += 1
                else:
                    ans2 += 1
        return min(ans1, ans2)

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理