[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 ...
随机推荐
- Servlet学习笔记05——什么是jsp?
1. jsp (java server page) (1)jsp是什么? sun公司制订的一种服务器端动态页面技术规范. 注: 因为虽然使用servlet也可以生成动态页面, 但是过于繁琐(需要使用o ...
- pycharm界面美化,个人喜欢
进入file-setting选项 界面设置主要是在appearance和editor里面.appearance主要是整个pycharm的主题设置,比如文件管理窗口的颜色,其实就是软件本身的主题设置.我 ...
- 如何在maven中的项目使用tomcat插件
在pom.xml中引入tomcat7插件,具体示例代码如下: <project> <build> <plugins> <plugin> <grou ...
- java从图片中识别文字
package com.dream.common; import java.awt.image.BufferedImage; import java.io.File; import java.io.I ...
- axios进行ajax请求得不到数据,cookie无法携带问题
这个坑也是很早之前踩过,今天做项目的时候居然忘了,怎么都拿不到数据,果然好记性不如烂笔头,决定写篇博客来祭奠下我的猪脑子: 原因可能就是你发送请求的时候,需要设置cookie,然而你的cookie并没 ...
- 【转载】C++中的static关键字的总结
本文前半部分转自:博主chao_yu 本文后半部分转自:博主VincentCZW 静态变量作用范围在一个文件内,程序开始时分配空间,结束时释放空间,默认初始化为0,使用时可以改变其值. 静态变量或静态 ...
- 微信小程序推广方案
拥有小程序只是基础,能玩转小程序运营才是关键.本文将会简单讲述十种最实用的小程序推广策略,结合具体案例阐述商家企业如何在拥有小程序后玩转小程序,快速实现小程序的推广. 一. 公众号+小程序 小程序可以 ...
- C指针——简单总结
简介: 指针变量在使用前,必须指向具体的有效的内存单元 指针变量在使用前不但要定义还要初始化 四个方面:指针的类型,指针指向的类型,指针的值或者指针所指向的内存区,指针本身所占的内存区 int *pt ...
- 笔记-爬虫-selenium常用方法
笔记-爬虫-selenium常用方法 1. 查找元素 常用的查找方法 find_element_by_name find_element_by_xpath find_element_by_l ...
- Eclipse+Tomcat7.0+MySQL 连接池设置
http://blog.sina.com.cn/s/blog_85d71fb70101ab99.html 工程名:JavaWeb 第一步:配置server.xml 在Tomcat的server.xml ...