Longest substring computational complexity

后端 未结 3 1032
臣服心动
臣服心动 2021-02-06 19:41

I was wondering about my apparently code for an MIT online edx class practice problem vs the provided answer code.

The assignment in question from the class was

3条回答
  •  生来不讨喜
    2021-02-06 20:06

    or, in one pass and without the overhead of string concatenation:

    length, start, stop, i = len(s), 0, 0, 0
    
    while i < length:
    
        j = i+1
    
        while j < length and s[j] >= s[j-1]:
            j += 1
    
        if j - i > stop - start:
            start, stop = i, j
    
        i = j
    
    print(s[start:stop])
    

提交回复
热议问题