【Leetcode】python - [3550] Smallest Index With Digit Sum Equal to Index 個人解法筆記

整理 LeetCode #3550 的個人解法筆記:解題思路、Time/Space Complexity 與邊界條件。

題目出處

3550. Smallest Index With Digit Sum Equal to Index

難度

easy

題目分類

Array, Math

2026-09-27 一刷

個人範例程式碼 - 一刷 (2026/09/27)

class Solution:
    def smallestIndex(self, nums: List[int]) -> int:
        for i, num in enumerate(nums):
            sum_digits = 0
            for digit in str(num):
                sum_digits += int(digit)

            if sum_digits == i:
                return i

        return -1

算法說明

把每一個數字加起來,可以用 % 10、 // 10 的數學方法把數字拆開,
或者這裡我用轉 str 再轉回 int 的方法把數字拆開。

Time Complexity

O(n) # nums 掃一遍

Space Complexity

O(1)

Licensed under CC BY-NC-SA 4.0
最後更新 Sep 27, 2026
使用 Hugo 建立
主題 Stack 由 Jimmy 設計