题目链接: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. jdbc:oracle:thin:@192.168.3.98:1521:orcl(详解)

    整理自互联网 一. jdbc:oracle:thin:@192.168.3.98:1521:orcljdbc:表示采用jdbc方式连接数据库oracle:表示连接的是oracle数据库thin:表示连 ...

  2. C#网络资源列表

    1, http://club.topsage.com/thread-371996-1-1.html

  3. DevExpress GridView属性设置 z

    本文主要总结控件的属性设置,附上图片,给大家一个参考.后续会给大家分享功能实现和使用的小技巧. GirdControl是数据的容器,它包含多种显示方式,GridView则是一种二维表格视图. 绑定数据 ...

  4. Headmaster's Headache

    题意: s门课程,现任老师有m个给出工资,和他们能教的课,现在有n个应聘的老师,给出费用和能教的课程标号,求使每门课都至少有两个老师教的最小花费 分析: n个老师选或不选有背包的特征,n很小想到用状压 ...

  5. jQuery遍历Table tr td td中包含标签

    function shengchen() { var arrTR = $("#tbModule").children(); var Context=""; $( ...

  6. canvas 模拟小球上抛运动的物理效果

    最近一直想用学的canvas做一个漂亮的小应用,但是,发现事情并不是想的那么简单.比如,游戏的逼真效果,需要自己来coding…… 所以,自己又先做了一个小demo,算是体验一下亲手打造物理引擎的感觉 ...

  7. Navicate

    快捷键 1.ctrl+q 打开查询窗口 2.ctrl+/ 注释sql语句 3.ctrl+shift +/ 解除注释 4.ctrl+r 运行查询窗口的sql语句 5.ctrl+shift+r 只运行选中 ...

  8. How to interact with the Chef Server using the Chef Server API using Shell script

    !/usr/bin/env bash   _chef_dir () { # Helper function: # Recursive function that searches for chef c ...

  9. Selection sort

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  10. Junit。。。

    keep the bar green to keep the code clean.