1103. 分糖果 II
1103. 分糖果 II
题目链接:1103. 分糖果 II
代码如下:
class Solution
{
public:vector<int> distributeCandies(int candies, int num_people) {vector<int> res(num_people,0);int count=1,i=0;//count代表此时对应第i个人需要分得糖果while(candies>0)//一直进行分糖{if(candies<count)//如果糖果不够就退出循环{res[i]+=candies;break;}candies-=count;res[i]+=count;i=(i+1)%num_people;//循环遍历count++;}return res;}
};