今天遇到的题都挺难的,不容易有会做的。

下面是代码,等明天看看Discuss里面有没有简单的方法~

Majority Element

My Submissions

Question
Total Accepted: 79833 Total Submissions: 208075 Difficulty: Easy

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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Java写法,遇到一个数字就标记一次,最后看谁的标记数目大于 ⌊ n/2 ⌋

 public class Solution {
public int majorityElement(int[] nums) {
int n = nums.length;
int[][] count = new int[n][2];
for (int i = 0; i < n; i++) {
count[i][1] = 0;
} count[0][0] = nums[0];
count[0][1] = 1;
int ss = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < ss; j++) {
if(count[j][0] == nums[i]) {
count[j][1]++;
}
else if (j == ss - 1) {
count[ss][0] = nums[i];
ss++;
}
}
}
int pan;
if(n % 2 == 0)
pan = n / 2;
else
pan = (n - 1) / 2;
for (int i = 0; i < n; i++) {
if(count[i][1] > pan)
return count[i][0];
} return 0;
}
}

【10_169】Majority Element的更多相关文章

  1. 【leetcode】Majority Element

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

  2. 【leetcode】Majority Element (easy)(*^__^*)

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

  3. 【数组】Majority Element II

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

  4. 【LeetCode 229】Majority Element II

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

  5. 【CF886E】Maximum Element DP

    [CF886E]Maximum Element 题意:小P有一个1-n的序列,他想找到整个序列中最大值的出现位置,但是他觉得O(n)扫一遍太慢了,所以它采用了如下方法: 1.逐个遍历每个元素,如果这个 ...

  6. 【LeetCode 169】Majority Element

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

  7. 【LeetCode OJ】Majority Element

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

  8. 【leetcode刷题笔记】Majority Element

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

  9. 【vue】vue +element 搭建及开发中项目中,遇到的错误提示

    1. import Layout from '@/views/layout/Layout'; export default [ { // 配置路由,当路径为'/activePublic',使用组件ac ...

随机推荐

  1. 2016-07-07: 重新编译时vc90.pdb不是创建此预编译头时使用的pdb文件

    使用VS2008在一个解决方案中包含多个项目时,当设置多个项目的中间目录为同一个目录时,在增量编译时出现"重新编译时vc90.pdb不是创建此预编译头时使用的pdb文件,请重新创建预编译头问 ...

  2. XidianOJ 1183 Water Problem: Items divided

    题目描述 Youyouyouyou is very interested in math, one day, an idea came into his mind that how many ways ...

  3. 万象客户端设置服务端ip保存在注册表的位置

    HKEY_LOCAL_MACHINE\SOFTWARE\Sicent\wx2004Clt 这个注册表被保护了,不能修改和删除,但可以在安全模式操作.

  4. xfire webServeic 例子

    xfire webServeic 例子,参考网上众多例子,自己写得完成了,给大家分享 大家只要按这个目录去建文件就可以了,然后运行,至于其中原理慢慢理会吧 环境:myeclipse 10 +xfire ...

  5. 接收 ajax POST 方式传入的参数

    ----前台--- var list = new Array(); var params = { gencodeid : "test001", value : "01&q ...

  6. html里文本编辑器如何制作呢?

    初入it职场,文本编辑器真的让人捉摸不透.最终在前端姐姐帮助下弄好了↓ 先在头部写好编辑器的各种功能的总体模型 <script>var editor; KindEditor.ready(f ...

  7. a==null和a.equals("null")的区别

    equals 是值比较,==是比较内存 A==B,比较句柄,就是比较变量A,B的地址存放的东西,比如int A=0;String B="bbbb";那么变量A的地址方的就是0,B的 ...

  8. 第一章 tomcat安装与启动

    一.安装 1.下载tomcat安装包 2.解压安装包 3.配置环境变量 打开~/.bash_profile文件,输入一下两句话: export TOMCAT_HOME=/Users/enniu1/De ...

  9. Servlet实现自动刷新功能

    使用Servlet实现自动刷新功能,每一秒钟在浏览器输出一个随机数字. package chensi.com; import java.io.IOException; import java.util ...

  10. 为什么for(int i=0;i<9;i++) 在c语言中是错误的?

    显示表示,i 变量不可以在for中定义,必须在外面定义,这是为什么? 因为C99标准以前的C标准是不支持临时变量在for循环中定义的. C99标准就支持这样写.但是目前有些编译器并不怎么愿意支持C99 ...