problem
理解题意:主要是计算candies的种类数。
如果大于candies数目的一半,则返回一半的值,否则,返回candies的种类数。
solution1:
class Solution { public: int distributeCandies(vector<int>& candies) { set<int> myset;//unordered_set<int> myset; for(int i=0; i<candies.size(); i++) { myset.insert(candies[i]); } return min(myset.size(), candies.size()/2); } };
注意掌握set的用法;
solution2:
class Solution { public: int distributeCandies(vector<int>& candies) { return min(unordered_set<int>(candies.begin(), candies.end()).size(), candies.size()/2); } };
re
1. Leetcode_easy_575. Distribute Candies;
end