【10_169】Majority Element
今天遇到的题都挺难的,不容易有会做的。
下面是代码,等明天看看Discuss里面有没有简单的方法~
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Java写法,遇到一个数字就标记一次,最后看谁的标记数目大于 ⌊ n/2 ⌋
public class Solution {
public int majorityElement(int[] nums) {
int n = nums.length;
int[][] count = new int[n][2];
for (int i = 0; i < n; i++) {
count[i][1] = 0;
} count[0][0] = nums[0];
count[0][1] = 1;
int ss = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < ss; j++) {
if(count[j][0] == nums[i]) {
count[j][1]++;
}
else if (j == ss - 1) {
count[ss][0] = nums[i];
ss++;
}
}
}
int pan;
if(n % 2 == 0)
pan = n / 2;
else
pan = (n - 1) / 2;
for (int i = 0; i < n; i++) {
if(count[i][1] > pan)
return count[i][0];
} return 0;
}
}
【10_169】Majority Element的更多相关文章
- 【leetcode】Majority Element
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
- 【leetcode】Majority Element (easy)(*^__^*)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【数组】Majority Element II
题目: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The alg ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【CF886E】Maximum Element DP
[CF886E]Maximum Element 题意:小P有一个1-n的序列,他想找到整个序列中最大值的出现位置,但是他觉得O(n)扫一遍太慢了,所以它采用了如下方法: 1.逐个遍历每个元素,如果这个 ...
- 【LeetCode 169】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode OJ】Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that app ...
- 【leetcode刷题笔记】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【vue】vue +element 搭建及开发中项目中,遇到的错误提示
1. import Layout from '@/views/layout/Layout'; export default [ { // 配置路由,当路径为'/activePublic',使用组件ac ...
随机推荐
- leetcode 125
125. Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric ...
- NHibernate系列文章十九:NHibernate关系之多对多关系(附程序下载)
摘要 NHibernate的多对多关系映射由many-to-many定义. 从这里下载本文的代码NHibernate Demo 1.修改数据库 添加Product表 添加ProductOrder表 数 ...
- projecteuler Problem 9 Special Pythagorean triplet
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 Fo ...
- pdo文字水印类,验证码类,缩略图类,logo类
文字水印类 image.class.php <?php /** * webrx.cn qq:7031633 * @author webrx * @copyright copyright (c) ...
- 登陆mysql时出现unknown variable 'character_set_client=UTF8' 的错误
今天,登陆数据库服务器的时候,出现了下面的错误: [root@localhost app]# mysql -uroot -p mysql: unknown variable 'character-se ...
- XidianOJ 1096 数的拆分
题目描述 输入自然数n,然后将其拆分成由若干数相加的形式,参与加法运算的数可以重复. 输入 多组数据.每组只有一个整数n,表示待拆分的自然数n. n<=80 输出 每组一个数,即所有方案数. - ...
- ArcGIS Wpf MarkerSymbol 图形符号无法序列化为 JSON
[问题贴,尚不知如何解决] 在GraphicsLayer中添加一个点,使用自定义模板渲染该点,在Vs2012设计界面可以看到,但运行时出现异常 代码如下: <esri:Graphic x:Nam ...
- X Window 设定介绍
在 Unix Like 上面的图形用户接口 (GUI) 被称为 X 或 X11 X11 是一个『软件』而不是一个操作系统: X11 是利用网络架构来进行图形接口的执行与绘制: 最著名的 X 版本为 X ...
- 【随笔】ARP和RARP
ARP协议是什么? ARP协议是"Address Resolution Protocol"(地址解析协议)的缩写.在局域网中,网络中实际传输的是"帧",帧里面是 ...
- Odoo启动过程
[本文基于odoo9源码编写] odoo包含的服务有 db object report workflow web[wsgi] Odoo以wsgi 规范提供Web及Web服务db/object/repo ...