From 1f578753add53d83efb27d4e9c46faca21e830d3 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Fri, 26 Jun 2020 15:18:53 +0800 Subject: [PATCH] Update --- README.md | 1 + problems/0575.分糖果.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 problems/0575.分糖果.md diff --git a/README.md b/README.md index a680aafd..c9a4a2ee 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ LeetCode 最强题解(持续更新中): |[0209.长度最小的子数组](https://github.com/youngyangyang04/leetcode/blob/master/problems/0209.长度最小的子数组.md) |数组 | **暴力** **滑动窗口**| |[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md) |链表 | **原链表移除** **添加虚拟节点** 递归| |[0383.赎金信](https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md) |数组 |**暴力** **字典计数**| +|[0575.分糖果.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md) |哈希表 |**模拟**| |[0707.设计链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0707.设计链表.md) |链表 |**模拟**| diff --git a/problems/0575.分糖果.md b/problems/0575.分糖果.md new file mode 100644 index 00000000..7f9c3966 --- /dev/null +++ b/problems/0575.分糖果.md @@ -0,0 +1,29 @@ +## 题目地址 +https://leetcode-cn.com/problems/distribute-candies/ + +## 思路 + +因为种类是可妹妹先来,所以思路先求出一共有多少种糖果,然后如果糖果种类大于candies的一半了,return candies的一半,否则,就是return 糖果的数量就可以了。 + + +## 代码 + +``` +class Solution { +public: + int distributeCandies(vector& candies) { + std::vector hashTable(200001, -1); // 初始化一个hashtable,因为数字的大小在范围[-100,000, 100,000]内,所以这个hashtable大小要是200001,这样才能取到200000这个下表索引 + for (int i = 0; i < candies.size(); i++) { + hashTable[candies[i] + 100000]++; + } + int count = 0; + for (int i = 0; i < hashTable.size(); i++) { + if (hashTable[i] != -1) { + count ++; + } + } + int half = candies.size() / 2; + return count > half ? half : count; + } +}; +```