题解 | 【模板】序列操作

【模板】序列操作

https://www.nowcoder.com/practice/12da4185c0bb45918cfdc3072e544069

from re import X
import sys
q =int(input())
lst = list()

for _ in range(q):
    s = list(map(int,input().split()))
    # s = sys.stdin.readline().split() #效果同上

    op = s[0]
    if op ==1:
        x=s[1]
        lst.append(x)
    elif op==2:
        if len(lst):
            lst.pop(-1)
    elif op==3:
        i=s[1]
        print(lst[i])
    elif op==4:
        i,x=s[1],s[2]
        lst.insert(i+1,x)#在i-i+1中间插入,实际上是新插入的为i+1
    elif op==5:
        lst.sort()
    elif op==6:
        lst.sort(reverse=True)#降序排列
        #reverse()反转列表
    elif op==7:
        print(len(lst))
    elif op==8:
        for i in lst:
            print(i,end=" ")
        print()


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务