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

@ -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;
}
};
```