【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 ...
随机推荐
- CodeBlocks配置pthread环境
参考资料:MinGW配置pthread环境 按[参考资料]里说的[下载资源]后,将libpthreadGC2.a放到codeBlocks安装目录下的MinGW\lib目录下,然后将pthread.h ...
- HTML4.01和XHTML1.0和XHTML1.1的一些区别
接触web前端以来,一直使用的都是html5,因此一直没搞明白HTML4.01和XHTML1.0和XHTML1.1之间的区别,今天在看<精通CSS>一书,有简单介绍这几个,在这儿记录下. ...
- maximo功能修改笔记
经过前几次的简单的修改系统功能,对maximo的bean开发已经有了一定了解,现在是耗时近两个礼拜来修改了一项系统功能,所用到的知识 Bean Fld, 下面我认真总结修改功能过程中的学到的知识: 目 ...
- 【原创】No matching distribution found for Twisted>=10.0.0 (from scrapy)
系统 Ubuntu14.04 python 2.7.11 运行 pip install scrapy 报错: No matching distribution found for Twiste ...
- 关于yaha中文分词(将中文分词后,结合TfidfVectorizer变成向量)
https://github.com/jannson/yaha # -*- coding: utf-8 -*- """ Created on Wed Aug 10 08: ...
- 编译caffe报错:_ZN5boost16exception_detail10bad_alloc_D2Ev
具体报错信息很长的. text._ZN5boost16exception_detail10bad_alloc_D2Ev[_ZN5boost16exception_detail10bad_alloc_D ...
- druid的安装
最近想玩druid.druid的底层是fastbit索引的列式存储.采用分布式的zookeeper调度.实时大数据分析软件.主要针对OLAP操作. 搭环境搭环境.druid的核心成员成立了一个叫imp ...
- mysql问题小结
1.数据表存在,但查询时提示不存在 原因:默认情况下,mysql在windows对表名大小不敏感(lower_case_table_names=1),在linux上大小敏感(lower_case_ta ...
- 使用nssm在windows服务器上部署nodejs
Linux上,可以轻松的使用forever或者pm2来部署nodejs应用.但是在windows下就麻烦了,pm2明确的说支持Linux & MacOS,forever在windows下貌似问 ...
- UVa 10071 - Back to High School Physics
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...