删除列表会导致下标不断变换,加入counter来计算前面删了几个数,每次删除时用原先的index-counter即可
class Solution:
def getNumber(self, a: List[int]) -> int:
def is_prime(num):
base = 2
if num == 1:
return False
elif num in (2,3):
return True
while base < num:
if num % base == 0:
return False
base += 1
return True
if len(a) == 1:
return a[0]
length = len(a)
counter = 0
index = 0
while index < length:
if is_prime(index):
i = index - counter
a.pop(i)
counter += 1
index += 1
return self.getNumber(a)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)