169:众数
注释:第三行用while比用if效率高
class Solution:
def majorityElement(self, nums: List[int]) -> int:
for i in set(nums):
while nums.count(i) > len(nums)/2:
return i
注释:第三行用while比用if效率高
class Solution:
def majorityElement(self, nums: List[int]) -> int:
for i in set(nums):
while nums.count(i) > len(nums)/2:
return i
Given an array of size n, find the majority element. The majority element is the element
给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 `⌊ n/2 ⌋` 的元素。 你可以假设数组是非空的,并且给定的数组总是存在众数。 示例 1:
题目链接:[https://leetcode-cn.com/problems/majority-element/description/][https_leetcode-cn.
给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 你可以假设数组是非空的,并且给定的数组总是存在众数。 示例 1: 输入
169. 求众数 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 你可以假设数组是非空的,并
题目: 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 你可以假设数组是非空的,并且给定的数组总是存在众数。 示例
转载链接:[https://blog.csdn.net/qq\_41231926/article/details/86479848][https_blog.csdn.net_q
试题 Given an array of size n, find the majority element. The majority element is the el
还没有评论,来说两句吧...