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

下面是代码,等明天看看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. ORA-27092: size of file exceeds file size limit of the process

    停数据库时遇到下述问题: $ ./addbctl.sh stop You are running addbctl.sh version 120.1 Shutting down database UAT ...

  2. 9.S5PV210的时钟系统

    1.时钟域:MSYS.DSYS.PSYS(1)因为S5PV210的时钟体系比较复杂,内部外设模块太多,因此把整个内部的时钟划分为3大块,叫做3个域.(2)MSYS: CPU(Cortex-A8内核). ...

  3. javascript generate a guid

    function Guid() { var random = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); retu ...

  4. bash脚本编程之一 变量、变量类型等

    变量的内容 1.变量命名:            1.只能包含字母.数字和下划线,并且不能以数字开头,    2.不应该跟系统中已有的环境变量重名    3.最好能见名知意 2.变量赋值: 设置变量: ...

  5. phpunit.xml

    <phpunit bootstrap="vendor/autoload.php"> <testsuites> <testsuite name=&quo ...

  6. EAS linux挂载数据盘

    查看数据盘名称 fdisk -l 假设没有挂载的数据盘为/dev/xvdb 格式化数据盘 mkfs.ext3 /dev/xvdb 添加自动挂载 mkdir /data echo '/dev/xvdb ...

  7. openssl命令用法

    openssl命令 配置文件:/etc/pki/tls/openssl.cnf 命令格式: openssl command [ command_opts ] [ command_args ] 众多子命 ...

  8. 线程本地变量ThreadLocal (耗时工具)

    线程本地变量类 package king; import java.util.ArrayList; import java.util.List; import java.util.Map; impor ...

  9. POJ 3348 - Cows 凸包面积

    求凸包面积.求结果后不用加绝对值,这是BBS()排序决定的. //Ps 熟练了template <class T>之后用起来真心方便= = //POJ 3348 //凸包面积 //1A 2 ...

  10. TransactionScope 事务使用说明

    TransactionScope是.Net Framework 2.0滞后,新增了一个名称空间.它的用途是为数据库访问提供了一个“轻量级”[区别于:SqlTransaction]的事物.使用之前必须添 ...