题目标签: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)的更多相关文章

  1. [LeetCode] 1103. Distribute Candies to People 分糖果

    题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5 ...

  2. LeetCode 1103. Distribute Candies to People

    1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...

  3. 【Leetcode_easy】1103. Distribute Candies to People

    problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...

  4. LeetCode 575. Distribute Candies (发糖果)

    Given an integer array with even length, where different numbers in this array represent different k ...

  5. 【leetcode】1103. Distribute Candies to People

    题目如下: We distribute some number of candies, to a row of n = num_people people in the following way: ...

  6. LeetCode 575 Distribute Candies 解题报告

    题目要求 Given an integer array with even length, where different numbers in this array represent differ ...

  7. LeetCode: 575 Distribute Candies(easy)

    题目: Given an integer array with even length, where different numbers in this array represent differe ...

  8. LeetCode.1103-向人们分发糖果(Distribute Candies to People)

    这是小川的第393次更新,第425篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第256题(顺位题号是1103).我们通过以下方式向一排n = num_people个人分 ...

  9. [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现

    [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...

随机推荐

  1. 【TCP】SYN攻击

    TCP握手协议 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接.第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确 ...

  2. Robot Framework:接口测试

    ---恢复内容开始--- robotframework进行接口测试,需要安装Request和RequestLibrary包 pip install requests pip install -U ro ...

  3. Shiro学习(14)SSL

    对于SSL的支持,Shiro只是判断当前url是否需要SSL登录,如果需要自动重定向到https进行访问. 首先生成数字证书,生成证书到D:\localhost.keystore 使用JDK的keyt ...

  4. docker的备份和迁移

    备份与迁移 容器保存为镜像 我们可以通过以下命令将容器保存为镜像 docker commit pyg_nginx mynginx pyg_nginx是容器名称 mynginx是新的镜像名称 此镜像的内 ...

  5. Redis Cluster集群详介绍和伪集群搭建

    1 什么是Redis-Cluster 为何要搭建Redis集群.Redis是在内存中保存数据的,而我们的电脑一般内存都不大,这也就意味着Redis不适合存储大数据,适合存储大数据的是Hadoop生态系 ...

  6. 一些基本LINUX命令以及测试环境的搭建

    LINUX操作系统平时用的不多,资深测试与开发同学们用的比较多,像我这样的一个人,只喜欢WINDOWS操作系统.但LINUX操作系统也用过一段时间, 知道一些基本命令,如果不记得的话,就百度一下,拿来 ...

  7. ()获取Cookies session

    [HttpGet] public string mo() { var httpRequest = HttpContext.Current.Request; var a = httpRequest.Co ...

  8. Java学习之DOS基础

    Dos命令行dir:列出当前目录下的文件和文件夹md :创建目录rd :删除目录cd :进入指定目录cd..:退回到上一级目录cd/:退回到根目录del:删除文件exit:退出dos命令行 进入dos ...

  9. ajax url地址

    当前网址 http://localhost:8080/exam_paper/402881ec5c3924ec015c394ee4210000/set_questions ajax请求url var u ...

  10. 3、第一个Appium测试

    运行脚本前环境准备: 1.IDE,推荐使用IJ 2.安装jdk环境,推荐>1.8 3.准备一台真机或者模拟器 4.SDK 5.maven环境 项目目录: CalculatorTest.java文 ...