V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
nyxsonsleep
V2EX  ›  问与答

牛客网 Python3 编译错误,求解

  •  
  •   nyxsonsleep · 323 天前 · 594 次点击
    这是一个创建于 323 天前的主题,其中的信息可能已经有所发展或是发生改变。

    从力扣刷过的题里似乎就换了参数名,力扣通过,牛客不通过,初始代码里的-> 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]
    
    3 条回复    2023-07-06 17:48:42 +08:00
    julyclyde
        1
    julyclyde  
       323 天前
    type hint 对版本有要求吧
    RuiCBai
        2
    RuiCBai  
       323 天前 via Android
    from typing import List
    Leonooo13
        3
    Leonooo13  
       321 天前
    试一下 list[int]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3190 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:39 · PVG 18:39 · LAX 03:39 · JFK 06:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.