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. 【转载】C++中的static关键字的总结

    本文前半部分转自:博主chao_yu 本文后半部分转自:博主VincentCZW 静态变量作用范围在一个文件内,程序开始时分配空间,结束时释放空间,默认初始化为0,使用时可以改变其值. 静态变量或静态 ...

  2. 传输控制协议(TCP)

    传输控制协议(TCP)[来自Unix网络编程(卷一)第2章] 1.TCP是一个面向连接.可靠性的传输协议: 2.TCP含有用于动态估算客户与服务器之间往返时间(RTT)的算法,以便它知道等待一个确认需 ...

  3. HTML+CSS : 笔记整理(3 移动端布局简单了解)

    流体布局:宽度用百分比,计算真实宽度用函数 : width: calc(25% - 4px); box-sizing: 1.content-box:默认计算方式 ,宽度和高度分别应用到元素的内容框.在 ...

  4. HyperLedger Fabric 1.4 区块链技术定义(2.1)

    区块链技术指使用点对点传输.共识机制.加密算法等技术,保证分布式数据库区块写入链中数据的一致性,达到去中心化和不可篡改的目的.       区块链就是一种特殊的分布式数据库,使用现有的各种成熟的技术, ...

  5. Too many parameters: expected 1, was given 2 Query: SELECT count(id) FROM `user` WHERE username = ?; Parameters: [org.apache.commons.dbutils.handlers.ScalarHandler@453da22c, [李明]]

    public Object getValue(String sql,Object... args) { Connection conn = null; Object obj= null; try { ...

  6. erlang中的原子(atom)内部实现[转]

    转自: http://www.kongqingquan.com/archives/208#more-208 Erlang中的atom由通用hash表实现,虚拟机中atom最终的用数值表示,对应表中的下 ...

  7. LDAP操作的两种方案

    最近由于项目需要研究了一下LDAP相关知识,感觉对没接触过的人来说还是有点坑的,所以记录下来给大家分享. 由于是第一次接触,就在网上搜了一些相关的文章,照着示例代码测试,却怎么也连不上LDAP服务器, ...

  8. 剑指Offer - 九度1521 - 二叉树的镜像

    剑指Offer - 九度1521 - 二叉树的镜像2013-11-30 23:32 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入 ...

  9. 《Cracking the Coding Interview》——第13章:C和C++——题目3

    2014-04-25 19:42 题目:C++中虚函数的工作原理? 解法:虚函数表?细节呢?要是懂汇编我就能钻的再深点了.我试着写了点测大小.打印指针地址之类的代码,能起点管中窥豹的作用,从编译器的外 ...

  10. DOS程序员手册(十三)

    744页 在DPMI 1.0下,系统会修改并重新装载所有含选择符的段寄存器,并且将所有 含有要释放的选择符的寄存器清空为0. 客户程序绝不能修改或释放该功能分配的任何描述符.Int 31h.功能010 ...