nyxsonsleep
V2EX  ›  问与答

牛客网 Python3 编译错误,求解

  •  
  •   nyxsonsleep · Jul 4, 2023 · 1147 views
    This topic created in 1067 days ago, the information mentioned may be changed or developed.

    从力扣刷过的题里似乎就换了参数名,力扣通过,牛客不通过,初始代码里的-> 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 replies    2023-07-06 17:48:42 +08:00
    julyclyde
        1
    julyclyde  
       Jul 4, 2023
    type hint 对版本有要求吧
    RuiCBai
        2
    RuiCBai  
       Jul 4, 2023 via Android
    from typing import List
    Leonooo13
        3
    Leonooo13  
       Jul 6, 2023
    试一下 list[int]
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5325 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 08:35 · PVG 16:35 · LAX 01:35 · JFK 04:35
    ♥ Do have faith in what you're doing.