题目链接: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.
*
*/
public class MajorityElement { // 40 / 40 test cases passed.
// Status: Accepted
// Runtime: 253 ms
// Submitted: 1 minute ago //时间复杂度为 O(n), 空间复杂度为 O(1) //因为最多的那个元素占一半及以上, 故能够和别的元素相互抵消,最后剩下的未抵消掉的元素为所求
//将数组分成三块:num[0...i]为 归并的 还未抵消的数组。 num[i+1...j-1]空暇区, num[j...num.length-1]为等待抵消的数组
//抵消规则:若num[i] = num[j] 则把num[j]归入num[0...i+1]数组中
// 若num[i] != num[j] 则把num[i] 抵消掉 然后 i-- static int majorityElement(int[] num) { int i = -1; for (int j = 0; j < num.length; j++) { if(i == -1) num[++i] = num[j];
else {
if(num[j] == num[i]) num[ ++i] = num[j];
else i --;
} }
return num[0];
}
public static void main(String[] args) { System.out.println(majorityElement(new int[]{4}));
System.out.println(majorityElement(new int[]{4, 4, 5}));
System.out.println(majorityElement(new int[]{4, 3, 3}));
System.out.println(majorityElement(new int[]{4, 3, 4}));
} }

169 Majority Element [LeetCode Java实现]的更多相关文章

  1. 169. Majority Element - LeetCode

    Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...

  2. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  3. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  4. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  5. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

  6. Week1 - 169.Majority Element

    这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...

  7. 169. Majority Element(C++)

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  8. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

  9. Java for LeetCode 169 Majority Element

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

随机推荐

  1. 高性能Server---Reactor模型-----Socket

    高性能Server---Reactor模型   原文地址:http://www.ivaneye.com/2016/07/23/iomodel.html 无处不在的C/S架构 在这个充斥着云的时代,我们 ...

  2. oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序

    15511477451 原文 oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序? 环境:win7 64位系统.oracle11g数据库 问题描述:在win7 64位系统 ...

  3. IAR编译信息分析

    1.怎么设置可以查看单片的内存(消耗)使用状况? IAR的菜单栏 -->Tools -->IDE Options -->Messages -->Show build messa ...

  4. 根据给定的日期给 dateEdit 控件增加颜色

    private void dateEdit1_DrawItem(object sender, DevExpress.XtraEditors.Calendar.CustomDrawDayNumberCe ...

  5. cocos2d-x 详解之 CCTexture2D(纹理图片)和 CCTextureCache(纹理缓存)

    精灵和动画都涉及到纹理图片的使用,所以在研究精灵与动画之前,我们先来了解一下纹理图片类CCTexture2D和纹理缓存CCTextureCache的原理: 当一张图片被加载到内存后,它是以纹理的形式存 ...

  6. dbms_file_transfer使用简介

    dbms_file_transfer这个包可以在两个位置传输文件,分别可以有以下位置: a 从一个asm diskgroup传输到另外一个asm diskgroup b 从一个asm diskgrou ...

  7. .net高级技术(class0515)

    本次课程中讲的有的东西都是根据初学者的认知规律进行了调整,并不是严谨的,比如很多地方在多AppDomain条件下很多说法就不对了,但是说严谨了大家就晕了,因此继续不严谨的讲吧. 很多面试题都在这阶段的 ...

  8. Hibernate逆向工程

    MySQL Administrator 创建表   MyEclipse Database Explorer视图: 1. New  2 .Driver template: MySQL Connector ...

  9. ubuntu java jdk安装及环境变量设置

    1.下载jdk1.7.0_79 (32位操作系统)jdk-7u79-linux-i586.tar.gz (64位操作系统)jdk-7u79-linux-x64.tar.gz http://www.or ...

  10. mysql的point类型查询处理

    mysql的point类型,很蛋疼的表示更习惯于key—value的lng,lat 假设不得不处理数据库字段poi是point类型,其中的数据为 : POINT(28.2789745229671 11 ...