★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-rqesatky-kq.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given an array nums
sorted in non-decreasing order, and a number target
, return True
if and only if target
is a majority element.git
A majority element is an element that appears more than N/2
times in an array of length N
. github
Example 1:數組
Input: nums = [2,4,5,5,5,5,5,6,6], target = 5 Output: true Explanation: The value 5 appears 5 times and the length of the array is 9. Thus, 5 is a majority element because 5 > 9/2 is true.
Example 2:微信
Input: nums = [10,100,101,101], target = 101 Output: false Explanation: The value 101 appears 2 times and the length of the array is 4. Thus, 101 is not a majority element because 2 > 4/2 is false.
Note:app
1 <= nums.length <= 1000
1 <= nums[i] <= 10^9
1 <= target <= 10^9
給出一個按 非遞減 順序排列的數組 nums
,和一個目標數值 target
。假如數組 nums
中絕大多數元素的數值都等於 target
,則返回 True
,不然請返回 False
。spa
所謂佔絕大多數,是指在長度爲 N
的數組中出現必須 超過 N/2
次。 code
示例 1:htm
輸入:nums = [2,4,5,5,5,5,5,6,6], target = 5 輸出:true 解釋: 數字 5 出現了 5 次,而數組的長度爲 9。 因此,5 在數組中佔絕大多數,由於 5 次 > 9/2。
示例 2:blog
輸入:nums = [10,100,101,101], target = 101 輸出:false 解釋: 數字 101 出現了 2 次,而數組的長度是 4。 因此,101 不是 數組佔絕大多數的元素,由於 2 次 = 4/2。
提示:
1 <= nums.length <= 1000
1 <= nums[i] <= 10^9
1 <= target <= 10^9
28 ms
1 class Solution { 2 func isMajorityElement(_ nums: [Int], _ target: Int) -> Bool { 3 var cnt:Int = 0 4 for x in nums 5 { 6 if x == target 7 { 8 cnt += 1 9 } 10 } 11 return cnt > nums.count/2 12 } 13 }