vivo技术岗提前批笔试,20200607,一小时3道题
1.给定一个数组 数组元素取值为0 1,比如A = [1,0,0,0,0],要求在1不能相邻的基础上尽可能把原数组中的0改为1,问最多多多少个1
比如A = [1,0,0,0,0] 那么修改为B = [1,0,1,0,1],增加了2个1
def solution01(A): n = len(A) count = 0 if n==1: if A[0]==0: return 1 else: return 0 for i in range(0,n): if i==0: if A[i]==0 and A[i+1]==0: count += 1 A[i] = 1 if 1<=i<n-1: if A[i-1]==0 and A[i]==0 and A[i+1]==0: count += 1 A[i]=1 if i==n-1: if A[i]==0 and A[i-1]==0: count += 1 A[i] = 1 return count2.leetcode 887 K个鸡蛋N层楼,最少试验多少次
3.leetcode 23 合并K个排序链表
全部评论
(4) 回帖