如何解决返回最大升序数组的索引
这里,我目前返回最大升序数组的长度。但是,我想修改它,以便我可以返回最大升序数组的索引。我尝试在 if (A[i] > A[i-1]) :
中设置 idx = I 但这似乎不起作用。我还能尝试什么?
def func(A):
n = len(A)
m = 1
l = 1
idx = -1
# traverse the array from the 2nd element
for i in range(1,n) :
# if current element if greater than prevIoUs
# element,then this element helps in building
# up the prevIoUs increasing subarray encountered
# so far
if (A[i] > A[i-1]) :
l =l + 1
idx = i-1
else :
# check if 'max' length is less than the length
# of the current increasing subarray. If true,# then update 'max'
if (m < l) :
m = l
# reset 'len' to 1 as from this element
# again the length of the new increasing
# subarray is being calculated
l = 1
# comparing the length of the last
# increasing subarray with 'max'
if (m < l) :
m = l
# required maximum length
return m
解决方法
我希望这就是您要找的。p>
def func(A):
n = len(A)
m = 1
l = 1
index = [0,0]
flag=0
for i in range(1,n):
if (A[i] > A[i-1]) :
l =l + 1
if flag==0:
flag=1
lindex = i-1
else:
if (m < l) :
m = l
index = [lindex,i-1]
flag=0
l = 1
if (m < l) :
m = l
index = [lindex,i]
return index
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。