从用户处获取整数输入,然后从一个数组中删除元素,该数组中有许多连续的ocurence。你知道吗
例如,输入数组是“aabcca”,用户的输入是2。 那么答案应该是“ba”。你知道吗
我在元素不重复的时候试过了。我的代码非常适合“aaabbcc”这样的例子。你知道吗
for j in range(t, (n+1)):
if (t == n):
if (count == k):
array = [x for x in array if x != temp]
print array
exit()
if (t == n and count == k):
array = [x for x in array if x != temp]
print array
exit()
if temp == data[j]:
count += 1
t += 1
if temp != data[j]:
if count == k:
array = [x for x in array if x != temp]
temp = data[t]
count = 1
t += 1
您可以使用
sliding window
或two pointers
来解决它。你知道吗关键是使用
[start, end]
范围来记录一个连续的seq,并且只添加长度小于n
的seq:测试和输出:
希望这对您有所帮助,如果您还有其他问题,请发表评论。:)
有一种方法:
相关问题 更多 >
编程相关推荐