Day22

改变一下学习策略,多看discuss,看博客总是乱七八糟。

  1. Move Zeroes
    For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
    Note:
    You must do this in-place without making a copy of the array.
    Minimize the total number of operations.
class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        for num in nums:
            if num == 0:
                nums.remove(num)
                nums.append(num)
  1. Majority Element
    思路:找出现次数超过数组长度一半的元素
class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return sorted(nums)[len(nums)//2]
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,487评论 0 23
  • 三毛流浪记 欣然 “三毛”,之前只是看过动画片版的、影视真人...
    yiyi_1650阅读 4,960评论 1 3
  • 夏至這一天,是白天最長的一天,也標志著炎夏的開始。 在中國最北端的黑龍江省漠河縣,夏至日太陽升起來,就像老頑童般不...
    大莲子阅读 2,540评论 0 1
  • 金秋送爽,丹桂飘香,农历九月初九日的重阳佳节,活动丰富,情趣盎然,有登高、赏菊、喝菊花酒、吃重阳糕、插茱萸等等。 ...
    5513刘一蕾阅读 1,417评论 1 1
  • 手机充电才拔下来,差点日记都不记得写了。今天算是回来来起的最晚的一天,9点多才起来下楼。午饭在外婆家吃的...
    天空蓝上阅读 793评论 0 0