【LeetCode 169】Majority Element
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.
思路:
找到一个数组中出现次数超过一半的数。排序、哈希等方法就不提了,考虑O(n)的复杂度,有一种传说中的摩尔投票算法可用。其实也很简单,初始选第一个元素,依次比对它和其它的元素,若相等则其票数加一,反之则减一,当其票数为0时,更换投票对象为当前的元素,继续执行,最终即可得结果(前提的数组中存在这样的数,否则结果不确定是什么,依赖于元素的摆放顺序)。
C++:
class Solution {
public:
int majorityElement(vector<int> &num) { int len = num.size();
if(len == )
return ; int cnt = , ret = num[];
for(int i = ; i < len; i++)
{
if(cnt == )
{
ret = num[i];
cnt++;
}
else
{
if(num[i] == ret)
cnt++;
else
cnt--;
}
}
return ret;
}
};
Python:
class Solution:
# @param {integer[]} nums
# @return {integer}
def majorityElement(self, nums):
ret, cnt = 0, 0 for val in nums:
if cnt == 0:
ret = val
cnt = 1
else:
if ret == val:
cnt = cnt + 1
else:
cnt = cnt - 1
return ret
【LeetCode 169】Majority Element的更多相关文章
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode OJ】Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that app ...
- Leetcode # 169, 229 Majority Element I and II
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode OJ】Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode题解】136_只出现一次的数字
目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- 【LeetCode题解】350_两个数组的交集Ⅱ
目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...
随机推荐
- Oracle 学习笔记(二)
1.创建用户,一般是具有dba权限的用户才能使用: create user 用户名 identified by 密码; 2.删除用户: drop user 用户名,注意,如果用户拥有对象,则不能直接删 ...
- C++primer中的TextQuery(读取文本)
本题目对应于 C++primer(第四版)中 第十章的文本查询类(TextQuery) 用到的知识有: 顺序容器 vector 关联容器 set,map 标准输入输出流,文件流,字符串流 //写一个文 ...
- 进程内核栈、用户栈及 Linux 进程栈和线程栈的区别
Linux 进程栈和线程栈的区别 http://www.cnblogs.com/luosongchao/p/3680312.html 总结:线程栈的空间开辟在所属进程的堆区,线程与其所属的进程共享进程 ...
- 保障视频4G传输的流畅性,海康威视摄像头相关设置
我们目前的rtsp视频解决方案如下: 摄像头<---------->NVR(通过4G上传)<---------->easydarwin<---------->自己的 ...
- 利用 __FUNCTION__ 宏打印函数调用信息
__FUNCTION__ 宏表示当前所在函数名: __FILE__ 宏表示当前所在文件路径: __LING__ 宏表示当前所在行: 利用对象离开函数时调用析构函数销毁的特点,打印出函数执行结束的信息 ...
- Hibernate 主键策略
Hibernate主键生成策略 .自动增长identity 适用于MySQL.DB2.MS SQL Server,采用数据库生成的主键,用于为long.short.int类型生成唯一标识 使用SQL ...
- Hibernate学习笔记(1)
1 使用Hibernate (1)创建User Library,命名为HIBERNATE3,加入需要的jar (2)创建hibernate配置文件hibernate.cfg.xml, 为了便于调试最好 ...
- VS2015中的异常配置
The New Exception Settings Window in Visual Studio 2015Managing Exceptions with the Debugger Underst ...
- java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Drawable;I)V in class Landroid/support/v4/graphics/drawable/DrawableCompat
Bug: java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Dra ...
- chrome 31删除输入框的历史记录
chrome 31删除输入框的历史记录 TMD居然要用Shift + delete 了.... 为毛!!!