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]

你可能感兴趣的:(Day22)