【10_169】Majority Element
今天遇到的题都挺难的,不容易有会做的。
下面是代码,等明天看看Discuss里面有没有简单的方法~
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的更多相关文章
- 【leetcode】Majority Element
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
- 【leetcode】Majority Element (easy)(*^__^*)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【数组】Majority Element II
题目: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The alg ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【CF886E】Maximum Element DP
[CF886E]Maximum Element 题意:小P有一个1-n的序列,他想找到整个序列中最大值的出现位置,但是他觉得O(n)扫一遍太慢了,所以它采用了如下方法: 1.逐个遍历每个元素,如果这个 ...
- 【LeetCode 169】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode OJ】Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that app ...
- 【leetcode刷题笔记】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【vue】vue +element 搭建及开发中项目中,遇到的错误提示
1. import Layout from '@/views/layout/Layout'; export default [ { // 配置路由,当路径为'/activePublic',使用组件ac ...
随机推荐
- VS2010 项目引用了DLL文件,也写了Using,但是编译时提示:未能找到类型或命名空间名称 <转>
昨天写了一个很小的winform程序,其中引用了自己写的两个dll文件. 本来认为轻松搞定,结果一编译居然提示:未能找到类型或命名空间名称..... 于是删掉两个dll重新引用,再编译结果依旧!很是郁 ...
- uva1587BOX
给定6个矩形的长和宽wi和hi(1≤wi,hi≤1000),判断它们能否构成长方体的6个面. 思路是首先排序,每个矩形都是x<y,就是短边x,长边y,然后对六个矩形进行二级排序,排序以后构成长方 ...
- JS 退出系统并跳转到登录界面的实现代码
js代码如下: <script language="javascript" type="text/javascript"> function log ...
- Http的请求对象
Servlet 客户端 HTTP 请求 当浏览器请求网页时,它会向 Web 服务器发送特定信息,这些信息不能被直接读取,因为这些信息是作为 HTTP 请求的头的一部分进行传输的.您可以查看 HTTP ...
- 用asp.net c# HttpWebRequest获取网页源代码
public string GetPage(string url) { HttpWebRequest request = null; HttpWebResponse response = null; ...
- weed-fs参数列表
weed-fs没有详细的帮助文档,为了方便阅读,特意把有用的参数帮助罗列出来.未列出的两个命令为version(版本查询) 及shell(这个命令在0.45版本只有回显功能)nerc@Ubuntu:~ ...
- C# 连接 Oracle 的几种方式
一:通过System.Data.OracleClient(需要安装Oracle客户端并配置tnsnames.ora)1. 添加命名空间System.Data.OracleClient引用2. usin ...
- android 根据包名打开app程序
如: 如打开微信: 查看包名的工具app:http://pan.baidu.com/s/1kVK2ER9 效果如下: 查看包名.版本和签名的工具app:http://pan.baidu.com/s/1 ...
- Log4Net日志的配置
<configuration> <configSections> <section name="log4net" type="log ...
- {Reship}{ListView}C# ListView用法详解
======================================================================== This aritcle came from http ...