Day22

作者: wendy_要努力努力再努力 | 来源:发表于2017-11-27 10:38 被阅读0次

    改变一下学习策略,多看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]
    

    相关文章

      网友评论

          本文标题:Day22

          本文链接:https://www.haomeiwen.com/subject/bnurbxtx.html