[LeetCode169]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.
题意为:找出最多的数是哪个数。
package com.leetcode.mair; import java.util.Arrays; public class Solution169 { public int majorityElement(int[] nums) { int mel = nums.length/2,count=1,me=1; Arrays.sort(nums);
me=nums[0];
for(int i=1; i<nums.length; i++){
if(nums[i-1] == nums[i]){
count++;
if(count>=mel){ me = nums[i];
mel = count;
}
}else
count=1;
} return me;
}
public static void main(String[] args) { int nums[]= {1};
System.out.println(new Solution169().majorityElement(nums));
}
}
[LeetCode169]Majority Element的更多相关文章
- LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III
LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...
- leetcode169——Majority Element (C++)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode169]Majority Element求一个数组中出现次数大于n/2的数
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- LeetCode169:Majority Element(Hash表\位操作未懂)
题目来源: Given an array of size n, find the majority element. The majority element is the element that ...
- [Swift]LeetCode169. 求众数 | Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【leetcode】Majority Element
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- windows系统下用VScode配置远程编辑服务器文件的环境!通过Rmate方法
虽然公司电脑win可以通过Xshell通过SSH远程连接家中内网linux服务器了,但是只能用vim编辑文件有点不爽. 于是上网查询,windows下使用vscode远程编辑服务器文件的办法.参照博文 ...
- Ubuntu 16.04上安装并配置Postfix作为只发送SMTP服务器
如果大家已经在使用第三方邮件服务方案发送并收取邮件,则无需运行自己的邮件服务器.然而,如果大家管理一套云服务器,且其中安装的应用需要发送邮件通知,那么运行一套本地只发送SMTP服务器则更为理想. 如何 ...
- Myeclipse代码格式化的样式和保存自动格式化
第一种方法:下载格式化代码样式文件,参考这位老兄的方法(包含了保存自动格式化):http://blog.csdn.net/u010028869/article/details/49780515 下 ...
- 【Theory of Generalization】林轩田机器学习基石
紧接上一讲的Break Point of H.有一个非常intuition的结论,如果break point在k取到了,那么k+1, k+2,... 都是break point. 那么除此之外,我们还 ...
- Python 绘制棋盘
import turtle pen = turtle.Pen() pen.speed(10) width = 30 # 格子宽度 count = 18 # 横向纵向格子数 o = width * co ...
- Java基础-5运算符
一).算数运算符: 算术运算符的功能是做各种算术运算,其操作数可以是字符型.整型或浮点型数据. 运算符 运算 示例 结果 备注 + 加 5+5 10 - 减 4-2 2 * 乘 2*3 6 既 ...
- sentry
https://docs.sentry.io/quickstart/?platform=javascript
- python3知识点之---------列表的介绍
1.列表是什么? 它是由一系列特定顺序排序的元素组成.元素可以表示一切任何的事物,元素之间可以没有任何关系.列表用方括号[ ] 表示,元素之间由逗号隔开. 例如表示一系列数字的列表: numbe ...
- ssm项目中ueditor富文本编辑器的使用
一.下载 https://ueditor.baidu.com/website/index.html 将ueditor放到项目中合适的位置 二 . 配置文件上传路径 在utf8-jsp/jsp/conf ...
- 过滤器(Filter)和Nuget
一.过滤器 AOP(面向切面编程)是一种架构思想,用于把公共的逻辑放到一个单独的地方,这样就不用每个地方都写重复的代码,比如程序中发生异常,不用每个地方都try catch 只要在(golbal的Ap ...