LeetCode 1103. Distribute Candies to People (分糖果 II)
题目标签:Math
题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完。
for loop可以计数糖果的数量,直到糖果发完。但是还是要遍历array 给people 发糖,这里要用到 index = (本轮分糖果的量 % people 的人数)糖果的数量从0 开始计数,这样的话,index 就会一直重复遍历 array,具体看code。
Java Solution:
Runtime: 1ms, faster than 90.53%
Memory Usage: 33.8 MB, less than 100.00%
完成日期:07/15/2019
关键点:利用%重复遍历array
class Solution {
public int[] distributeCandies(int candies, int num_people) {
int[] people = new int[num_people];
for(int give = 0; candies > 0; candies -= give) {
people[give % num_people] += Math.min(candies, ++give);
}
return people;
}
}
参考资料:LeetCode discuss
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 1103. Distribute Candies to People (分糖果 II)的更多相关文章
- [LeetCode] 1103. Distribute Candies to People 分糖果
题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5 ...
- LeetCode 1103. Distribute Candies to People
1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...
- 【Leetcode_easy】1103. Distribute Candies to People
problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...
- LeetCode 575. Distribute Candies (发糖果)
Given an integer array with even length, where different numbers in this array represent different k ...
- 【leetcode】1103. Distribute Candies to People
题目如下: We distribute some number of candies, to a row of n = num_people people in the following way: ...
- LeetCode 575 Distribute Candies 解题报告
题目要求 Given an integer array with even length, where different numbers in this array represent differ ...
- LeetCode: 575 Distribute Candies(easy)
题目: Given an integer array with even length, where different numbers in this array represent differe ...
- LeetCode.1103-向人们分发糖果(Distribute Candies to People)
这是小川的第393次更新,第425篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第256题(顺位题号是1103).我们通过以下方式向一排n = num_people个人分 ...
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...
随机推荐
- Java——抽象:abstract
3.4抽象:abstract 3.4.1什么是抽象类? 随着继承层次中一个个新子类的定义,类变得越来越具体,而父类则更一般,更通用.类的设计应该保证父类和子类能够共享特征.有时将一个父类设计得非常抽象 ...
- JVM内存分为哪几部分?各个部分的作用是什么?
JVM内存区域分为五个部分,分别是堆,方法区,虚拟机栈,本地方法栈,程序计数器. 堆. 堆是Java对象的存储区域,任何用new字段分配的Java对象实例和数组,都被分配在堆上,Java堆可使用-Xm ...
- 带头节点的单链表-------C语言实现
/***************************************************** Author:Simon_Kly Version:0.1 Date:20170520 De ...
- 洛谷 P3803 【模板】多项式乘法(FFT)
题目链接:P3803 [模板]多项式乘法(FFT) 题意 给定一个 \(n\) 次多项式 \(F(x)\) 和一个 \(m\) 次多项式 \(G(x)\),求 \(F(x)\) 和 \(G(x)\) ...
- 静态成员 static 能被继承吗
在类定义中,它的成员(包括数据成员和 成员函数)可以用关键字static声明为静 态的,这些成员称为静态成员 静态成员的特性: • 不管这个类创建了多少个对象,静态成员只有一个拷贝,这个拷贝被所有属于 ...
- Java8中Map的遍历方式总结
在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行. public class LambdaMap { private Map< ...
- centos7.5下生成公钥,实现ssh免密钥登陆
配置SSH无密码登录需要4步准备工作生成公钥和私钥导入公钥到认证文件,更改权限测试1. 准备工作确认本机sshd的配置文件(需要root权限) # vi /etc/ssh/sshd_config 1找 ...
- 30-Ubuntu-用户权限-01-用户和权限的基本概念
1.用户 用户是Linux系统工作中重要的一环,用户管理包括用户和组管理. 在Linux系统中,不论是由本机或是远程管理登录系统,每个系统都必须拥有一个账号,并且对于不同的系统资源拥有不同的使用权限. ...
- docker配置阿里云镜像加速地址
1.注册阿里云用户 2.阿里云控制台首页,开发者工具->开发者中心 3.加入开发者中心,并设置密码. 4.镜像中心->镜像加速器
- Use on Git
Preface The document is about to introduce some specialties on PLM development and mainte ...