【问题标题】:Find all array subsequences of a given value查找给定值的所有数组子序列
【发布时间】:2016-04-20 04:14:26
【问题描述】:

我正在寻找一个给出如下列表的算法:

[1, 1, 2, 1, 1, 5, 1, 1, 1, 1, 2, 1]

可以找到并返回给定值的所有子序列。例如,如果给定值 1,该函数将返回 [[1, 1], [1, 1], [1, 1, 1, 1], [1]]

我相信这类似于对数组的所有子序列求和或查找给定字符串的所有子序列等问题,但算法从来都不是我的强项。答案可以是伪代码或与语言无关。如果您不介意,您能解释一下解决方案的复杂性吗?

如果有帮助,我可以解释我需要这个做什么。如果需要,请发表评论。

【问题讨论】:

  • 你能解释一下你的所有子列表应该有value 1吗? ......这里的财产是什么? (我最好的猜测:你想要其中所有值最多为 1 的所有子序列)
  • 另外:上次我检查[1,1,1,1,1,1,1,1,1] 也将是一个子序列see the definition
  • 是的,这将是一个子序列,但我正在查看的所有解决方案都是关于总结一个子序列或找到最长的子序列,而不是返回实际序列本身。不,1 只是我选择的任意示例。
  • 所以你只想在有元素neq 1时拆分列表?
  • 不,我正在做一些比这更复杂的事情,与 iOS 中的 CoreVideo 和 AVFoundation 有关,我只是想知道如何在高层次上完成这项工作。

标签: arrays algorithm subsequence


【解决方案1】:

我们可以通过扫描数组两次以 O(n) 的时间复杂度做到这一点。伪代码:

//use an array list so we can access element at an index in O(1) time
outputArrays = new ArrayList<int[]> //list of arrays

//loop to declare arrays of outputs - this scans each element once
int currLen = 0;
for (item in inputArray) {
 if (item = itemToLookFor) {
  currLen++;
 }else if (currLen > 0) {
  currLen = 0;
  outputArrays.add(new int[currLen]);
 }
}

//loop to actually populate the output - this scans each element once
currLen = 0;
currIndex = 0;
for (item in inputArray) {
 if (item = itemToLookFor) {
  outputArrays.getElement(currIndex)[currLen] = item;
  currLen++;
 }else if (currLen > 0) {
  currLen = 0;
  currIndex++;
 }
}

如果有什么我可以澄清的,请告诉我。

【讨论】:

  • 所以首先构建输出数组,然后填充数组。有什么原因不能在一个循环内完成吗?或许,有没有更实用的方法来解决这个问题?
  • @startupthekid none - 当然还有一种实用的方式 - 一旦你真正说出你想要的东西,它就会用任何合理的 FP 语言用单线来表达 - 问题是只有你想要 @987654322 @ 或简单的splitBy
  • 好吧,我想我真正想要的是所有通过过滤条件的子序列。比如说过滤器是{ x % 2 == 0 },输入数组是[1, 2, 2, 3, 4, 4],如果有意义的话,输出就是[[2, 2], [4, 4]]
  • @startupthekid 我敢肯定有很多方法可以在本地执行此操作,可能只需一次调用。我这样回答是因为我认为您想查看算法(因为已标记问题),而不是库调用。如果您不需要将输出作为数组,您可以一次性完成。
  • @startupthekid 满足条件没问题 - 只需创建一个辅助方法来检查给定输入的条件并用您的辅助方法替换 if (item = itemToLookFor) 行。
【解决方案2】:

a 为初始数组,res - 生成的序列数组,curSeq - 当前序列,given_value - 给定值。

res = []
curSeq = []
for i = 1..length(a)
    if a[i] != given_value
        if curSeq has at least one item
            append curSeq to res
        end if
        curSeq = []
    else
        append given_value to curSeq
    end if
end for
if curSeq has at least one item
    append curSeq to res
end if

如您所见,时间复杂度为 O(n),其中 n 是初始数组的长度。

【讨论】:

    【解决方案3】:

    这是O(n) 解决方案。 这里arr 是序列的输入数组,sequence 是子序列的数组。您可以为您的答案保存序列另一个数组。

    arr = [1, 1, 2, 1, 1, 5, 1, 1, 1, 1, 2, 1]; // here is your
    selectNumber = 1 //take input for selected input
    sequence = [];
    for (i: 0 to arr.length) {
      if (arr[i] == selectNumber) {
        sequence.push(selectNumber);
      } else {
        if(sequence.length > 0) {
          print sequence;
          sequence = [] // empty sequence array as it is already printed or saved
        }
      }
    }
    
    if (sequence > 0) {
      print sequence; // last sequence if exist
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      相关资源
      最近更新 更多