[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 ...
随机推荐
- Maven - pom.xml常用元素
基本坐标信息:
- layer 的功能
1.layer.alert() layer.alert('',{ title: "<div style='color:red;margin-left:20px;font-size:20 ...
- ajax状态值和状态码
AJAX状态值是指,运行AJAX所经历过的几种状态,无论访问是否成功都将响应的步骤,可以理解成为AJAX运行步骤.如:正在发送,正在响应等,由AJAX对象与服务器交互时所得:使用“ajax.ready ...
- ### Cause: java.lang.reflect.UndeclaredThrowableException
### Cause: java.lang.reflect.UndeclaredThrowableException Caused by: org.apache.ibatis.exceptions.Pe ...
- mybatis在where中比较复杂的判断
<if test="param.applicationStateInNumber != null and param.applicationStateInNumber != ''&qu ...
- centos安装xfce及输入法
一.执行CentOS7 最小安装 去官网 https://www.centos.org/ 下载CentOS-7-x86_64-Minimal-1804.iso,然后使用rufus刻录U盘,安装之.安装 ...
- POJ:2739-Sum of Consecutive Prime Numbers(尺取)
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27853 Ac ...
- talent-aio源码阅读小记(一)
近来在oschina上看到一个很火的java 即时通讯项目talent-aio,恰巧想了解一下这方面的东西,就阅读了一下项目的源码,这里对自己阅读源码后的一些心得体会做一下备忘,也希望能够对其他项目中 ...
- idea录制宏
录制一个热部署的快捷键 1.打开Edit-->Macros-->statr Macro Recording 打开之后idea右下角就会出现一个小圆点 然后就可以开始录制自己想要的快捷键 按 ...
- 9,Flask 中的蓝图(BluePrint)
蓝图,听起来就是一个很宏伟的东西,在Flask中的蓝图 blueprint 也是非常宏伟的,它的作用就是将 功能 与 主服务 分开. 比如说,你有一个客户管理系统,最开始的时候,只有一个查看客户列表的 ...