简体   繁体   English

当条件满足时,Python break语句不会终止程序

[英]Python break statement will not terminate program when condition meets

I am trying to solve Two Sum II on Leet Code. 我试图在Leet Code上解决Two Sum II。 The input is a sorted ascending list and a target value. 输入是排序升序列表和目标值。 My algorithm is supposed to output two indices of the elements that adds up to the target value. 我的算法应该输出两个元素索引,它们加起来就是目标值。 I am trying to use binary search for my implementation and here is my code: 我正在尝试使用二进制搜索来实现我的代码:

def twoSum(nums, target):
    total_array_index = len(nums)-1

    current_index = 0

    binary_start_index = current_index + 1
    binary_end_index = total_array_index
    binary_mid_index = (binary_start_index+binary_end_index)/2

    current_sum = nums[current_index]+nums[binary_mid_index]


    while current_sum is not target:
        #exit out the loop if no solution is found
        if current_index == total_array_index:
            print('no solution found!')
            break

        while (binary_start_index<=binary_end_index):
            #condition meets, but "break" statement does not terminate loop ONLY when input list is large
            if current_sum == target:
                current_index = current_index-1
                break

            if current_sum > target:
                binary_end_index = binary_mid_index - 1
                binary_mid_index = (binary_start_index+binary_end_index)/2
                current_sum = nums[current_index]+nums[binary_mid_index]

            else:
                binary_start_index = binary_mid_index + 1
                binary_mid_index = (binary_start_index+binary_end_index)/2
                current_sum = nums[current_index]+nums[binary_mid_index]


        current_index = current_index + 1
        binary_start_index = current_index + 1
        binary_end_index = total_array_index
        current_sum = nums[current_index]+nums[binary_mid_index]


    return [current_index,binary_mid_index]

While I did passed 7/16 tests, but it times out on the 8th test with a really large input. 虽然我确实通过了7/16测试,但它在第8次测试时超时,输入非常大。 I do not think it's the algorithm's issue. 我不认为这是算法的问题。 After trying to manually print out both 'current_index' and 'binary_mid_index', my program did successfully found the right indices that gave me the correct 'current_sum'. 在尝试手动打印'current_index'和'binary_mid_index'后,我的程序成功找到了正确的索引,它给了我正确的'current_sum'。 But the program simply won't 'break' out of the while loop when the input list is large. 但是当输入列表很大时,程序根本不会“打破”while循环。 Is this something relate to Python? 这与Python有关吗?

Here is the testing input: 这是测试输入:

nums=[12,13,23,28,43,44,59,60,61,68,70,86,88,92,124,125,136,168,173,173,180,199,212,221,227,230,277,282,306,314,316,321,325,328,336,337,363,365,368,370,370,371,375,384,387,394,400,404,414,422,422,427,430,435,457,493,506,527,531,538,541,546,568,583,585,587,650,652,677,691,730,737,740,751,755,764,778,783,785,789,794,803,809,815,847,858,863,863,874,887,896,916,920,926,927,930,933,957,981,997]

target = 542

break only breaks you out of its current while loop, not the outer one. break只会让你摆脱当前的循环while不是外循环。

You use multiple while s - you need to set the inner break condition on the outer while as well and combine its 2 conditions in a way that it evaluates to false when the inner breaked. 你可以使用多个while - 你需要在外部设置内部中断条件,并且在内部中断while将其评估为false的方式组合它的两个条件。

Alternatively set a breakOuter = FALSE before the first while, set breakOuter = True on the inner while s directly before you break and add and breakOuter == false to the outer while condition 可替换地设置一个breakOuter = FALSE也先之前,设置breakOuter = True在内while你S前直接break并添加and breakOuter == false到外部while条件

I think that the simplest way is to just use 我认为最简单的方法就是使用

return 

instead of 代替

break 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM