题目如下:
我的代码有什么问题,代码如下:
class Solution:
def punishmentNumber(self, n: int) -> int:
dp = [0] * (n + 1)
for i in range(1, n + 1):
num_str = str(i * i)
for j in range(1, len(num_str) + 1):
x = int(num_str[:j])
if x > i:
break
if x == i or num_str[j:] == '':
dp[i] += i * i
elif dp[i - x] != 0 and str(dp[i - x]) == num_str[j:]:
dp[i] += i * i
return sum(dp)