Majority Element (leetcode )
来源:互联网 发布:河南数据统计采集门户 编辑:程序博客网 时间:2024/04/30 08:01
题目:
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
解题思路:
由于没有对时间复杂度进行要求,可以直接将数组排序。找出中间那个元素即为主元素。
代码:
class Solution:
# @param num, a list of integers
# @return an integer
def majorityElement(self, num):
n = len(num)
num.sort()
return num[n/2]
0 0
- 【leetcode】Majority Element II,Majority element algorithm
- Leetcode: Majority Element &Majority Element II
- Leetcode Majority Element
- leetcode 169-Majority Element
- 【leetcode 哈希表】Majority Element
- LeetCode:Majority Element
- leetcode:Majority Element
- leetcode: Majority Element
- [LeetCode] Majority Element
- leetcode 153: Majority Element
- leetcode Majority Element
- LeetCode:Majority Element
- leetcode:Majority Element
- [LeetCode] Majority Element
- Leetcode-Majority Element
- [leetcode]Majority Element
- [LeetCode]Majority Element
- Leetcode: Majority Element
- AngularJS 无限滚动加载数据控件 ngInfiniteScroll
- Vmware vSphere(Esxi)常见问题汇总
- 2014十大顶级开源ERP系统点评
- Spring中Ioc入门实例
- 快速建立列表对话框
- Majority Element (leetcode )
- mahout基于物品的协同过滤指令
- VMware vSphere ESXI/ESX 中的HA工作原理介绍 .
- Android 类似于ArcMenu(github上开源项目)的自定义多个按钮视图
- Apache配置httpd-vhosts虚拟主机总结及注意事项
- android和iOS平台的崩溃捕获和收集
- C++学习 std::tr1::shared_ptr、std::tr1::weak_ptr及std::tr1::enable_shared_from_this
- 转:java读取properties文件方法和对比
- 照相机获取图片的两种形式