牛客网 Python3 编译错误,求解

350 天前
 nyxsonsleep

从力扣刷过的题里似乎就换了参数名,力扣通过,牛客不通过,初始代码里的-> List[int]飘红。

我试着在自己的 ide 上编辑了一下也通过了,牛客网点调试就一直提示“调试运行出错,退出码:1”,这让人怎么调试。 网址 practice/20ef0972485e41019e39543e8e895b7f?tpId=117&&tqId=37756

我在牛客网看到好几种编译器的格式了,不统一一下吗。。

牛客网

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]: #  -> List[int]飘红
        for i in range(len(numbers)):
            # 计算需要找到的下一个目标数字
            res = target-numbers[i]
                # 遍历剩下的元素,查找是否存在该数字
            if res in numbers[i+1:]:
                # 若存在,返回答案。这里由于是两数之和,可采用.index()方法
                # 获得目标元素在 nums[i+1:]这个子数组中的索引后,还需加上 i+1 才是该元素在 nums 中的索引
                return [i, numbers[i+1:].index(res)+i+1]

力扣

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums)):
            # 计算需要找到的下一个目标数字
            res = target-nums[i]
                # 遍历剩下的元素,查找是否存在该数字
            if res in nums[i+1:]:
                # 若存在,返回答案。这里由于是两数之和,可采用.index()方法
                # 获得目标元素在 nums[i+1:]这个子数组中的索引后,还需加上 i+1 才是该元素在 nums 中的索引
                return [i, nums[i+1:].index(res)+i+1]
614 次点击
所在节点    问与答
3 条回复
julyclyde
350 天前
type hint 对版本有要求吧
RuiCBai
350 天前
from typing import List
Leonooo13
348 天前
试一下 list[int]

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/953962

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX