题目在这:https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk/
思路分析:
这道题居然是中等难度题, 对于python来说,应该是简单难度。其他语言可能要考虑类型溢出的情况?
简单分析一下,就是无限循环使用粉笔,到几号不够用了就输出几号位置的下标。
上来直接用总粉笔数 除以 列表中的数值和。 得到的余数遍历一次即可得到答案。
完整代码:
class Solution:
def chalkReplacer(self, chalk: List[int], k: int) -> int:
count = sum(chalk)
print(count)
temp = k%count
print(temp)
num = 0
for i in range(len(chalk)):
num += chalk[i]
if num > temp:
return i
提交: