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.
Hide Tags: Divide and Conquer Array Bit Manipulation
解题思路:
(1)使用HashMap。Map的特点:不同意反复元素,因此在存储前须要推断是否存在
(2)推断HashMap中存在nums[i],假设存在。使用hm.get(nums[i])获取value,即通过key来获得value值,即count(出现的次数)
(3)假设count大于数组长度的一般。即返回该元素
(4)假设count不满足条件,向HashMap存储元素以及出现的次数。
代码例如以下:
public static int majorityElement(int[] nums)
{
/*
* Map的特点:不同意反复元素。因此在存储前须要推断是否存在
*/
Map<Integer, Integer> hm=new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++)
{
int count=0;
//推断HashMap中存在nums[i],假设存在,使用hm.get(nums[i])获取value
//即通过key来获得value值,即count(出现的次数)
if (hm.containsKey(nums[i]))
{
count=hm.get(nums[i])+1;
}
else
{
count=1;
}
//假设count大于数组长度的一般,即返回该元素
if (count>nums.length/2)
{
return nums[i];
}
//向HashMap存储元素以及出现的次数
hm.put(nums[i], count);
}
return 0; }
leetcode——169 Majority Element(数组中出现次数过半的元素)的更多相关文章
- 剑指Offer:找出数组中出现次数超过一半的元素
题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...
- LINQ 获取当前数组中出现次数最多的元素
LINQ 获取当前数组中出现次数最多的元素 1 List<string> a = new List<string>(); a.Add( ...
- python查找数组中出现次数最多的元素
方法1-np.argmax(np.bincount()) 看一个例子 array = [0,1,2,2,3,4,4,4,5,6] print(np.bincount(array)) print(np. ...
- 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 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- LeetCode 169 Majority Element 解题报告
题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...
- LeetCode 169. Majority Element解题方法
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- Dream_Spark版本定制第一课
从今天起,我们踏上了新的Spark学习旅途.我们的目标是要像Spark官方机构那样有能力去定制Spark. 一. 我们最开始将从Spark Streaming入手. 为何从Spark Streami ...
- Kth Smallest Number in Sorted Matrix
Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...
- HTTPS-加密SSL证书
从第一部分HTTP工作原理中,我们可以了解到HTTPS核心的一个部分是数据传输之前的握手,握手过程中确定了数据加密的密码.在握手过程中,网站会向浏览器发送SSL证书,SSL证书和我们日常用的身份证类似 ...
- webRTC视频通话,https协议,录制端和播放端
最近做视频直播模块,在网上也看到很多大神写的代码,写的都不错,但不是我想要的,有的可能比较老,不支持https协议,有的又将直播端和显示端放在一个程序中,不利于我使用,则本篇着重添加了https协议( ...
- PHP isset()、empty()、is_null()的使用区别详解
PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...
- LeetCode(24): 两两交换链表中的节点
Medium! 题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说 ...
- final在类和方法中的使用
package final0; //final修饰的类不能继承//final修饰的方法不能继承public class TestFinal3 { public static void main(Str ...
- django中的view测试和models测试样例
感觉用model_mommy比factory_boy要好些. 如果Models.py如下: from django.db import models from django.contrib.auth. ...
- PowerDesigner的安装
1.下载 2.步骤 3.效果 二:破解 1.参考文档 https://www.7down.com/soft/180716.html 2.说明 主要是一个文件,替换掉文件中的文件即可.
- php 会话控制(了解cookie与session之间的区别与联系)
相同点: 都可以在解决HTTP无状态的问题,使同一个客户端在访问网站的多次请求中,可以保存,设置信息,并且在请求事物之间建立联系. 不同点: 简单的说cookie的信息保存在客户端,session的信 ...