TypeError: 'builtin_function_or_method' object is not subscriptable

今天在做leetcode 821时,报出标题所示的错误,结果懵逼了半天也没明白,题目如下:

Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.

Example 1:

Input: S = "loveleetcode", C = 'e'
Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

报错前的代码如下:

import math
class Solution:
    def shortestToChar(self, S, C):
        """
        :type S: str
        :type C: str
        :rtype: List[int]
        """
        n = len(S)
        res = [n]*n
        pos = S.index(C)
        for i in range(n):
            if S[i] == C:
                pos=i
            res[i] = abs[pos-i] #报错行在这里
        S1 = S[::-1]
        pos = S1.index(C)
        for i in range(n):
            if S1[i] ==C:
                pos=i
            res[n-i-1] = min(res[n-i-1],abs(pos-i))
            
        return res

#报错信息为Line 15: TypeError: 'builtin_function_or_method' object is not subscriptable

原谅我一时脑袋秀逗了,abs()方法为小括号啊,不是[]

你可能感兴趣的:(TypeError: 'builtin_function_or_method' object is not subscriptable)