博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode169]Majority Element
阅读量:4448 次
发布时间:2019-06-07

本文共 849 字,大约阅读时间需要 2 分钟。

Majority Element

 
Total Accepted: 58500 Total Submissions: 163658
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
=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)); }}

  

转载于:https://www.cnblogs.com/lzeffort/p/4776082.html

你可能感兴趣的文章
MySQL for Excel —— 用Excel方式操作MySQL
查看>>
在 Cloud 9 中搭建和运行 Go
查看>>
求字符串中第一个只出现一次的字符
查看>>
ASP.NET常见模块:在线文件管理模块的设计与开发
查看>>
Swift学习第二天
查看>>
div 旋转
查看>>
【设计模式】4、原型模式
查看>>
进入meta模式关闭背光灯
查看>>
pycharm+PyQt5+python最新开发环境配置
查看>>
走进AngularJS
查看>>
【学习笔记】 唐大仕—Java程序设计 第5讲 深入理解Java语言之5.2 多态及虚方法调用...
查看>>
轻松实现Ecshop商城多语言切换
查看>>
async & await 的前世今生(Updated)
查看>>
iOS开发:用SQLite3存储和读取数据
查看>>
webstorm上svn的安装使用
查看>>
setAdapter(adapter)空指针nullPointer 解决办法 分类: ...
查看>>
【JEECG技术文档】数据权限自定义SQL表达式用法说明
查看>>
使用 Bootstrap Typeahead 组件
查看>>
第一次玩蛇,有点紧张。
查看>>
DAO层,Service层,Controller层、View层 的分工合作
查看>>