LeetCode 781. Rabbits in Forest (森林中的兔子)
题目标签:HashMap
题目给了我们一组数字,每一个数字代表着这只兔子说 有多少只一样颜色的兔子。
我们把每一个数字和它出现的次数都存入map。然后遍历map,来判断到底有多少个一样颜色的group,因为这里可能会出现一种情况:同一种颜色的兔子,可能有好几组。
所以利用 value / groupSize 和 value % groupSize 来判断,到底有多少只兔子需要计算,具体看code。
Java Solution:
Runtime: 3 ms, faster than 86.90%
Memory Usage: 37.8 MB, less than 17.65%
完成日期:03/28/2019
关键点:利用 value 和 groupSize 之间的关系,来判断准确的数量
class Solution {
public int numRabbits(int[] answers) {
Map<Integer, Integer> map = new HashMap<>();
int result = 0;
for(int answer : answers)
{
if(answer == 0)
result++;
else
map.put(answer, map.getOrDefault(answer, 0) + 1);
}
int groupCount = 0;
int leftover = 0;
for(int key : map.keySet())
{
int value = map.get(key);
int groupSize = key + 1;
groupCount = value / groupSize;
leftover = value % groupSize;
if(leftover > 0)
groupCount++;
result += groupCount * groupSize;
}
return result;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 781. Rabbits in Forest (森林中的兔子)的更多相关文章
- [LeetCode每日一题]781. 森林中的兔子
[LeetCode每日一题]781. 森林中的兔子 问题 森林中,每个兔子都有颜色.其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色.我们将这些回答放在 answers 数组里. ...
- Java实现 LeetCode 781 森林中的兔子(分析题)
781. 森林中的兔子 森林中,每个兔子都有颜色.其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色.我们将这些回答放在 answers 数组里. 返回森林中兔子的最少数量. 示例: ...
- 【python】Leetcode每日一题-森林中的兔子
[python]Leetcode每日一题-森林中的兔子 [题目描述] 森林中,每个兔子都有颜色.其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色.我们将这些回答放在 answers ...
- [Swift]LeetCode781. 森林中的兔子 | Rabbits in Forest
In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how ...
- LC 781. Rabbits in Forest
In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how ...
- 【LeetCode】781. Rabbits in Forest 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Rabbits in Forest 森林里的兔子
In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how ...
- 【LeetCode题解】94_二叉树的中序遍历
目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...
- #调整随机森林的参数(调整n_estimators随机森林中树的数量默认10个树,精度递增显著,但并不是越多越好),加上verbose=True,显示进程使用信息
#调整随机森林的参数(调整n_estimators随机森林中树的数量默认10个树,精度递增显著) from sklearn import datasets X, y = datasets.make_c ...
随机推荐
- Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1
记录一次傻逼的问题, 自己把自己蠢哭:Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1 错误说明: 返回的1是影响的行数,并不是自增的主键id: 想要获取自增主键id,需要 ...
- XAMPP--Apache服务无法启动问题定位及处理
一.问题简述: XAMPP 在使用一段时间后,Apache服务无法启动. 二.详细描述: 安装Xampp服务器套件之后,部署使用正常.一段时间未使用,再次打开时,Apache服务无法启动.错误提示如下 ...
- jq微信分享
(function() { var weChat = { init: function() { this.getData(); }, getData: function() { $.ajax({ ty ...
- jsTree插件简介(三)
UI-plugin JSTree的UI插件:用来处理选择.不选和鼠标悬浮树选项的插件. 一.属性包括: 1.select_limit:指定一次可以选中几个节点,默认为-1,表示无限制选中. 2.sel ...
- Queries for Number of Palindromes(求任意子列的回文数)
H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...
- 安卓app测试之Monkey测试
一.Monkey特点 1.运行时机:一般是产品稳定后 首轮功能测试完成的夜间进行 2.需要知道packageName 3.目的:主要测试产品是否存在崩溃问题和ANR问题. 二.获取包名的两个方法 首先 ...
- acedssget F 方式
ads_point p1; ads_point p2; acedGetPoint(NULL, _T("\n插入第一点"), p1); acedGetPoint(p1, _T(&qu ...
- pageHelper详解
详见:https://github.com/pagehelper/Mybatis-PageHelper/edit/master/wikis/zh/HowToUse.md ## 使用方法 1. 引入分页 ...
- 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList
package algorithms; import java.util.ArrayList; import java.util.Stack; /** * public class ListNode ...
- MS SQL Server查询 本日、本周、本月、本季度、本年起始时间
参数声明 declare @beginTime datetime, --查询开始时间 @endTime datetime, --查询结束时间 @queryTimeType tinyint; --查询时 ...