LeetCode 169. 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.
分析:
遍历数组,每当发现一对儿不相同的element时就成对儿删除,最后剩下的一定是个数过半儿的element。
public class Solution { public int majorityElement(int[] nums) { int count = 0; int majElem = 0; for(int i=0; i<nums.length; i++) { if(count == 0) { majElem = nums[i]; count++; } else { if(majElem == nums[i]) { count++; } else { count--; } } } return majElem; } }
LeetCode 169. Majority Element的更多相关文章
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element (众数)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169 Majority Element 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java for LeetCode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- LintCode Find Minimum In Rotated Sorted Array
1. 画图, 直观. 2. 讨论数组为空或者个数为零. 3. 讨论首尾, 若为翻转过的则进行查找直到最后两个数进行比较, 取小者. public class Solution { /** * @par ...
- akka优势
1.提供可扩展的实时事务处理. 2.为以下目标设计: 垂直扩展(并发) 水平扩展(远程调用) 高容错 3.Akka的核心,Akka-actor非常小的,可以非常方便地放进你的应用中,提供你需要的异步无 ...
- DMA控制器
DMA控制器依赖于平台硬件,这里只对i386的8237 DMA控制器做简单的说明,它有两个控制器,8个通道,具体说明如下: 控制器1: 通道0-3,字节操作, 端口为 00-1F 控制器2: 通道 4 ...
- 初学JAVA 感想
开始学习任何一门课(包括java),兴趣最重要.一直觉得自己在学计算机编程语言,学习了很多,但每门语言都停留在知识边缘地带,都没深入到它们的精华部分,对它们的精华部分知之甚少,于是趁学校开设这门课,并 ...
- Android深度探索HAL和驱动开发(卷1) 第一章 Android系统移植和驱动开发
由于Android是基于Linux内核的,因此,Android和其他Linux系统的核心部分差异非常小.然而不同版本的Android使用的Linux内核的版本有细微的差异,所以不同Android驱动可 ...
- #ifndef 的用法
背景: 头件的中的#ifndef,这是一个很关键的东西.比如你有两个C文件,这两个C文件都include了同一个头文件.而编译时,这两个C文件要一同编译成一个可运行文件,会引起大量的声明冲突,这时候需 ...
- 关于vue.js中class与style绑定的学习
练习代码: html: <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- HDU-2296 Ring(AC自动机+DP)
题目大意:给出的m个字符串都有一个权值.用小写字母构造一个长度不超过n的字符串S,如果S包含子串s,则S获取s的权值.输出具有最大权值的最小字符串S. 题目分析:先建立AC自动机.定义状态dp(ste ...
- LeetCode-Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Python之路,day9-Python基础
回顾:抽象方法@staticmethod 不能访问类的任何属性@classmethod 类方法 只能访问公有属性@property 属性方法 , 把一个方法变成一个静态属性def sayhi() pa ...