Python:每日一题 307
今天的题目:给定一个整数数组 nums 和一个整数 k。如果某个连续子数组中恰好有 k 个奇数数字,我们就认为这个子数组是 “优美子数组”。
返回这个数组中 “优美子数组” 的数目。
示例 1:
输入:nums = ,k = 3
输出:2
解释:包含 3 个奇数的子数组是 和 。
示例 2:
输入:nums = ,k = 1
输出:0
解释:数列中不包含任何奇数,所以不存在优美子数组。
示例 3:
输入:nums = ,k = 2
输出:16
{:10_298:}欢迎大家一起答题!{:10_298:} def func307(l:list,k):
l.append(1)
l2=[]
ret=0
v=1
for i in l:
if i % 2==0:
v+=1
continue
l2.append(v)
v=1
if (len(l2)<=k):
continue
ret+=l2[-1]*l2
l2.pop(0)
return ret
def f307(nums,k):
res=0
s=[-1]
for i in range(len(nums)):
if nums%2:
s.append(i)
s+=
for i in range(1,len(s)-k):
res += (s-s)*(s-s)
return res list1=
k=2
def f(x):
lst0=[]
while len(x)>0:
for i in range(len(x)):
lst0.append(x)
x=x
return lst0
def f1(x):
n=0
for i in x:
if i%2==1:
n+=1
return n
def main():
lst0=f(list1)
lst1=[]
for i in lst0:
if f1(i)==k:
lst1.append(i)
print(lst1)
print(len(lst1))
return lst1,len(lst1)
if __name__=='__main__':
main() 子数组若出现多次 是不是只算一次?比如 nums = ,k = 2
是不是 只有 共6个?
试探.jpg
def solve(nums:'list of int',k:int)->int:
le = len(nums)
pos = %2]
lt = len(pos)
if lt < k:
return 0
res = (pos+1)*((pos if lt>k else le)-pos)
#print('调试',res,pos)
for i in range(1,lt-k):
each = pos
#print('调试',each)
res += (each-each)*(each[-1]-each[-2])
else:
res += ((le-pos[-1])*(pos[-k]-pos[-k-1]))if lt>k else 0
return res
if __name__ == '__main__':
print('示例1 输出:',solve(nums = ,k = 3))
print('示例2 输出:',solve(nums = ,k = 1))
print('示例3 输出:',solve(,k = 2))
塔利班 发表于 2020-1-16 21:35
咱俩想到一块儿去了,话说为啥不用列表推导式呢? TJBEST 发表于 2020-1-16 22:26
子数组若出现多次 是不是只算一次?比如 nums = ,k = 2
是不是 只有
不是。 def fun307(nums , k):
M = len(nums)
jiIndexArr = %2 ]
N = len(jiIndexArr)
if N < k:
return 0
elif N == k:
return (jiIndexArr + 1)*(M - jiIndexArr)
else:
res = 0
res += (jiIndexArr + 1)*(jiIndexArr - jiIndexArr)
for i in range(1,N - k):
res += (jiIndexArr - jiIndexArr)*(jiIndexArr - jiIndexArr)
res += (jiIndexArr-jiIndexArr)*(M - jiIndexArr)
return res 本帖最后由 kinkon 于 2020-1-17 11:30 编辑
不知道会不会超时
from itertools import combinations as ct
def f307(nums, k):
if len(nums) < k or k == 0: return 0
cout, res = 0, []
nums = list(enumerate(nums))
for num in range(len(nums)):
res +=
if nums%2 != 0:cout += 1
if cout < k:return 0
res = sorted(res)
t1, t2 = 0, 0
for i in res:
for j in range(len(i)-1):
if i - i == 1:
if i%2 != 0:
t1 += 1
if t1 == k: t2 += 1
return t2 本帖最后由 zltzlt 于 2020-1-17 18:03 编辑
kinkon 发表于 2020-1-17 11:27
不知道会不会超时
解答错误
输入:nums = , k = 2
输出:3
打印:[((0, 2), (1, 2)), ((0, 2), (1, 2), (2, 2)), ((0, 2), (1, 2), (2, 2), (3, 1)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (6, 1)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (6, 1), (7, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (6, 1), (7, 2), (8, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (6, 1), (7, 2), (8, 2), (9, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (6, 1), (7, 2), (9, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (6, 1), (8, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (6, 1), (8, 2), (9, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (6, 1), (9, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (7, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (7, 2), (8, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (7, 2), (8, 2), (9, 2)), ((0, 2), (1, 2), (2, 2), (3, 1), (4, 2), (5, 2), (7, 2), (9, 2)), ((0, ...(被和谐)
预期结果:16 本帖最后由 fan1993423 于 2020-1-17 14:17 编辑
def fun307(lst,k):
odd=%2]
new_odd=[-1]+odd+
result=0
for i in range(len(odd)-k+1):
start=new_odd-new_odd
end=new_odd-new_odd
result+=start*end
return result def solve(A:'haha',k:int):
first,count,end,result,c = 0,0,0,0,0
end , m = first+k,k
while True:
for i in A:
if i%2 == 1:
count+=1
ifcount == m:
result+=1
if end - first == len(A) and c == 0:
break
if end == len(A):
c+=1
k+=1
first = 0
end = first+k
if c==0:
first+=1
end = first+k
c = 0
count = 0
return result
if __name__ == '__main__':
print('示例1 输出:',solve(A = ,k = 3))
print('示例2 输出:',solve(A = ,k = 1))
print('示例2 输出:',solve(A = ,k = 2))
一杯茶一包烟,一个BUG改一天!
又是一个暴力破解法def func307(arr, k):
res = 0
odd_or_even =
for i in range(0, len(arr)):
for j in range(len(arr), i+1, -1):
if arr.count(1) == k:
res += 1
return res
if __name__ == '__main__':
print('示例一:', func307(, 3))
print('示例二:', func307(, 1))
print('示例三:', func307(, 2)) def func307(arr, k):
res = 0
odd_or_even =
for i in range(0, len(arr)):
for j in range(len(arr), i+1, -1):
if arr.count(1) == k:
res += 1
return res
if __name__ == '__main__':
print('示例一:', func307(, 3))
print('示例二:', func307(, 1))
print('示例三:', func307(, 2)) def f307(L:list,k:int)->int:
count=0
for i in range(len(L)-1):
for j in range(i,len(L)+1):
if len(L)-].count(0)==k:
count+=1
return count
L =
k = 2
print(f307(L,k)) def fun307(nums,k):
def odd_count(x,k):
count = 0
for i in x:
if i%2:
count += 1
if count > k:
return 'over'
if count < k:
return 'less'
return 'equals'
result = []
for i in range(len(nums)-k+1):
for j in range(i+k,len(nums)+1):
if odd_count(nums,k) == 'equals':
result.append(nums)
if odd_count(nums,k) == 'over':
break
return len(result) 想问一下,每日一题是不再更新了嘛{:5_100:}
页:
[1]