This commit is contained in:
youngyangyang04
2020-06-26 15:18:53 +08:00
parent 41f25e8a55
commit 1f578753ad
2 changed files with 30 additions and 0 deletions

View File

@ -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) |链表 |**模拟**|

View File

@ -0,0 +1,29 @@
## 题目地址
https://leetcode-cn.com/problems/distribute-candies/
## 思路
因为种类是可妹妹先来所以思路先求出一共有多少种糖果然后如果糖果种类大于candies的一半了return candies的一半否则就是return 糖果的数量就可以了。
## 代码
```
class Solution {
public:
int distributeCandies(vector<int>& candies) {
std::vector<int> 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;
}
};
```