Majority Element

Total Accepted: 58500 Total Submissions: 163658My Submissions

Question Solution 

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的更多相关文章

  1. 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 ...

  2. leetcode169——Majority Element (C++)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. [LeetCode169]Majority Element求一个数组中出现次数大于n/2的数

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  4. LeetCode169:Majority Element(Hash表\位操作未懂)

    题目来源: Given an array of size n, find the majority element. The majority element is the element that ...

  5. [Swift]LeetCode169. 求众数 | Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  7. [LeetCode] Majority Element 求众数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. 【leetcode】Majority Element

    题目概述: Given an array of size n, find the majority element. The majority element is the element that ...

  9. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. spring-传统AOP

    Spring传统AOP AOP的增强类型 AOP联盟定义了Advice(org.aopalliance.aop.Interface.Advice) 五类(目标类方法的连接点): 1.  前置通知(or ...

  2. 在ubuntu上安装subline

    Sublime Text is a most popular, lightweight and smart cross-platform text and source code editor wit ...

  3. java的有用基础知识(2013-05-02-bd 写的日志迁移

    JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库.是java开发工具包 jre是java的运行环境(如果不做开发就不用安装jdk单独安装jre就可以运行java程序 ...

  4. 如果文件里是汉字的话,这地方seek括号里面只能是偶数

    >>> f=open("E:/pythonLearn/140.txt") >>> f.seek(8) #如果文件里是汉字的话,这地方seek括号 ...

  5. 笔记-爬虫-scrapy-srcapy-redis组件

    笔记-爬虫-scrapy-srcapy-redis组件 1.      简介 scrapy是一个爬虫框架,但不支持分布式,scrapy-redis是为了更方便的实现scrapy分布式爬虫的组件. 可以 ...

  6. MySQL CONCAT()与GROUP_CONCAT()的使用

      1 . MySQL CONCAT(str1,str2, ...)  --返回连接的字符串 mysql> SELECT CONCAT('My', 'S', 'QL'); -> 'MySQ ...

  7. vue命令集合

    创建vuecli脚手架:npm install -g @vue/cli拉取2的版本:npm install -g @vue/cli-init 创建webpack:npm i webpack@3.12. ...

  8. [记读书笔]python3.5实现socket通讯(UDP)

    UDP连接: 无连接,从一个端向另一端发送独立的数据分组 使用UDP连接的客户-服务器程序: UDPServer.py import socket serverPort = 50009 serverS ...

  9. 3-1 练习 HTML 总结

    1.段落 #段落 <div class="ui segment"> </div> #翻转 <div class="ui inverted s ...

  10. 2037: [Sdoi2008]Sue的小球

    2037: [Sdoi2008]Sue的小球 链接 题解 论文 代码 #include<cstdio> #include<algorithm> #include<cstr ...