本文介绍: 双指针法,设置l = 1,r = 2。不知道为什么一个例子过不去)=X 输出 l++并且r++解法一:暴力破解,果然超时终止条件是l <= X/2。解法二:我去看看视频解析

1.战神的对称谜题

知道为什么超时!

def main():
    s = input()
    result = 0
    for i in range(len(s)):
        l = i - 1
        r = i + 1
        while l >= 0 and r < len(s) and s[l] == s[r]:
            result = max(result, r - l + 1)
            l -= 1
            r += 1

        l = i
        r = i + 1
        while l >= 0 and r < len(s) and s[l] == s[r]:
            result = max(result, r - l + 1)
            l -= 1
            r += 1
    print(result)


if __name__ == '__main__':
    main();

2.元音字母

def main():
    s = input()
    n = int(input())
    vowel = ["a", "e", "i", "o", "u"]
    # print(s)
    result = 0
    for i in range(n):
        if s[i] in vowel:
            result += 1
    l = 0
    r = n - 1
    tmp = result
    while r + 1 < len(s):

        if s[l] in vowel and s[r + 1] not in vowel:
            tmp -= 1
        if s[r + 1] in vowel and s[l] not in vowel:
            tmp += 1
        if tmp &gt; result:
            result = tmp
        l += 1
        r += 1
    print(result)

if __name__ == '__main__':
    main();

3.

5.求和

解法一:暴力破解,果然超时


def main():
    result =[]
    X = int(input())
    for i in range(1,X):
        for j in range(i + 1, X):
            sum = int((i + j)*(j - i + 1)/2)
            # print(sum)
            if sum == X:
                # print(i,j)
                tmp = []
                tmp.append(i)
                tmp.append(j)
                result.append(tmp)
    for i in result:
        for j in i:
            print(j,end=" ")
        print("")

if __name__ == '__main__':
    main();

解法二:我去看看视频解析

指针法,设置l = 1,r = 2
<X r++
=X 输出 l++并且r++
&gt;X l++

终止条件是l <= X/2

(!不知道为什么一个例子过不去)


def main():
    result =[]
    X = int(input())
    l = 1
    r = 2
    while l <= X/2:
        sum = int((l + r) * (r - l + 1) / 2)
        if sum == X:
            tmp = []
            tmp.append(l)
            tmp.append(r)
            result.append(tmp)
            l += 1
            r += 1
            continue
        elif sum < X:
            r += 1
            continue
        else:
            l += 1
            continue

    for i in result:
        for j in i:
            print(j,end=" ")
        print("")

if __name__ == '__main__':
    main();

原文地址:https://blog.csdn.net/weixin_53908842/article/details/134739309

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_27120.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注