3550. Smallest Index With Digit Sum Equal to Index

约 1 分钟

LeetCode 每日一题:Smallest Index With Digit Sum Equal to Index

本页目录

LeetCode 原题

You are given an integer array nums.

Return the smallest index i such that the sum of the digits of nums[i] is equal to i.

If no such index exists, return -1.

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 1000

思路:暴力枚举

没什么好说的,直接翻译题目条件即可

python
for i, num in enumerate(nums):
    if sum(map(int, str(num))) == i:
        return i
return -1

复杂度

  • 时间复杂度:O(n), 遍历一遍数组
  • 空间复杂度

返回文章列表