Day8-169. Majority Element

今日题目:169. Majority Element(easy)

Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

Example 1:
Input: nums = [3,2,3]
Output: 3

Example 2:
Input: nums = [2,2,1,1,1,2,2]
Output: 2

Constraints:

  • n == nums.length
  • 1 <= n <= 5 * 104
  • -231 <= nums[i] <= 231 - 1
    Follow-up: Could you solve the problem in linear time and in O(1) space?

My solution

class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        nums.sort()
        return nums[len(nums)//2]

Result

https://ithelp.ithome.com.tw/upload/images/20210923/20140843alXg6CVwxP.png


<<:  Day 23 - Speech Synthesis

>>:  [Part 2 ] Vue.js 的精随-元件 Props

Flutter体验 Day 27-flame SpriteComponent

flame SpriteComponent 看着团队挑战的成员写了一篇 从零开始的8-bit迷宫探险...

【Day 12】Rich Menu 主选单

我们拿 YouBike 微笑单车 官方帐号为例。 红色框框我们都会称作为「主选单」,很多官方帐号都...

第50天~

这个得上一篇:https://ithelp.ithome.com.tw/articles/10258...

javascript(addEventListener事件处理函式)(DAY21)

这篇文章会介绍addEventListener事件处理函式,它其实和event的监听事件很像,但是a...

day 14 - drone 的go-test & golangci-lint

自己的产出自己负责!每次交付专案之前我都会想到这句话, 是不是该再检查一下 程序码已经成功的buil...