鱼C论坛

 找回密码
 立即注册
查看: 1299|回复: 0

[技术交流] 【朱迪的LeetCode刷题笔记】169. Majority Element #Easy #Python

[复制链接]
发表于 2023-6-6 13:26:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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
-109 <= nums[i] <= 109

Follow-up:
Could you solve the problem in linear time and in O(1) space?

Judy
Python divide and conquer  runtime beats 57.23% memory beats 8.57% taught by cs341
  1. class Solution(object):
  2.     def majorityElement(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: int
  6.         """
  7.         h = len(nums) // 2
  8.         
  9.         def f(lst):
  10.             l = len(lst)  
  11.             m = l // 2
  12.             
  13.             if l % 2 == 1 and nums.count(lst[-1]) > h:
  14.                 return lst[-1]            
  15.             
  16.             if l == 1:
  17.                 return lst[0]

  18.             if l == 2:
  19.                 if nums.count(lst[0]) > h:
  20.                     return lst[0]
  21.                 return lst[1]

  22.             nlst = []
  23.             
  24.             for i in range(0, m):
  25.                 if lst[i] == lst[i+m]:
  26.                     nlst.append(lst[i])
  27.             
  28.             return f(nlst)
  29.         
  30.         return f(nums)
复制代码

Python boyer more majority vote algorithm runtime beats 65.21% memory beats 67.84% taught by sol
  1. class Solution(object):
  2.     def majorityElement(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: int
  6.         """
  7.         
  8.         x = 0
  9.         c = 0

  10.         for n in nums:
  11.             if c == 0:
  12.                 x = n
  13.                 c += 1
  14.             elif x == n:
  15.                 c += 1
  16.             else:
  17.                 c -= 1
  18.         
  19.         return x
复制代码


Sol1
  1. class Solution {
  2. public:
  3.     int majorityElement(vector<int>& nums) {
  4.         int count = 0;
  5.         int candidate = 0;

  6.         for(int i = 0; i < nums.size(); i++){
  7.             if (count == 0){
  8.                 candidate = nums[i];
  9.             }
  10.             if(nums[i] == candidate){
  11.                 count+=1;
  12.             }else {
  13.                 count-=1;
  14.             }
  15.         }
  16.         return candidate;
  17.     }
  18. };
复制代码


2-Figure14.1-1.png


本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 03:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表