小编Oli*_*een的帖子

替换多个\t

    switch(start)
   {case 0:printf("");
          j=1;
          break;
    case 1:printf("\t");
          j=2;
          break;
    case 2:printf("\t\t");
          j=3;
          break;
    case 3:printf("\t\t\t");
          j=4;
          break;
    case 4:printf("\t\t\t\t");
          j=5;
          break;
    case 5:printf("\t\t\t\t\t");
          j=6;
          break;
    case 6:printf("\t\t\t\t\t\t");
          j=7;
          break;
   }
Run Code Online (Sandbox Code Playgroud)

开始接受用户的输入,有什么办法可以缩短这段代码???????任何帮助表示赞赏!!!!!!!!

c

3
推荐指数
1
解决办法
143
查看次数

偶数异或子数组的数量

用偶数异或找出子数组的数量(子数组的异或意味着其元素的异或)。例如:

l=[1,2,3,4]   # ----> Answer of this is 4
Run Code Online (Sandbox Code Playgroud)

(解释:[2],[4],[1,2,3],[1,2,3,4]--->这些是异或偶数的子数组,因此子数组数=4)

这是我的代码:

def odd_int(lst):
    odd=0
    for i in lst:
        if i%2!=0:
            odd+=1 
    return odd    
l=list(map(int,input().split()))
x=0 # Denotes number of even xor arrays
for i in range(len(l)):
    for j in range(i,len(l)):
        l1=l[i:j+1]
        y=odd_int(l1)
        if y%2==0:
            x+=1
print(x)
Run Code Online (Sandbox Code Playgroud)

上面的代码超过了时间限制,所以有什么建议可以将它优化为 O(n)???谢谢你的时间!!!

python algorithm performance dynamic-programming python-3.x

3
推荐指数
1
解决办法
241
查看次数