Aintor 2021-11-09 07:46 采纳率: 0%
浏览 46
已结题

leetcode Two Sum的submit输出异常(python3)

题目如下:
Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.


class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        x = 0
        y = x+1
        while True:
            if nums[x] + nums[y] == target:
                return [x,y]
                break
            else:
                try:
                    print(nums[y+1])
                except IndexError:
                    x += 1
                    y = x+1
                else:
                    y += 1

按理说应该是返回一个长度为2的列表,可是submit提示Output Limit Exceeded

img

run code是可以的,但submit其中有两个test一直卡着过不去,请问各位这是什么原因?

  • 写回答

1条回答 默认 最新

  • python收藏家 2021-11-09 09:26
    关注

    测试的输入是什么 ,如果不存在两个元素和等于target怎么处理的 一直循环?

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月13日
  • 修改了问题 11月11日
  • 修改了问题 11月9日
  • 创建了问题 11月9日

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?