【问题标题】:Finding depth of specified element in an array of arrays (or list of lists etc.)在数组数组(或列表列表等)中查找指定元素的深度
【发布时间】:2017-07-14 10:39:11
【问题描述】:

我必须创建一个函数(使用伪代码),它返回数组中指定元素的深度(内部包含可选数组),例如:

def array[] = {"a", {"b", {"c"}, "d"}, {{{}}}, "e"};

对于 "e" 它应该返回 0,对于 "c" 它应该返回 2 等等。 如果数组中没有指定元素,函数应该返回-1。

我已经尝试了几次,但我不知道优雅(和工作..)的解决方案,只有这个:

func foo(array[], var element) {
   int depth = 0;
   boolean found = false;
   boolean recursion = false;
   boolean foundInRecursion = false;

   for (int i = 0; i < array.length; i++) {
        if (array[i] instanceof array[]) {
            depth++;
            recursion = true;
            foo(array[i], element);
            recursion = false;
        } else if (array[i] == element) {
            if (recursion) {
                foundInRecursion = true;
            } else {
                found = true;
            }
        }
   }

   if (foundInRecursion) {
       return depth;
   } else if (found){
       return 0;
   } else {
       return -1;
   }
}

非常感谢任何帮助! 谢谢

【问题讨论】:

    标签: arrays algorithm multidimensional-array pseudocode


    【解决方案1】:

    在你的伪代码中:

    func depth(array[], var element) {
        /* this function returns how deep the element is in the given array */
        for (int i = 0; i < array.length; i++) {
            current = array[i]
    
            if current == element {
                 return 0; // element is right in the array we are given
            }
    
            if (current instanceof array[]) {
                // Whoa, subarray! How deep our element is in it?
                subdepth = depth(current, element)
                if subdepth >= 0 {
                    // Element was found in *sub*-array, so add 1 to the depth
                    return subdepth + 1;
                }
            }
        }
        // If we are here, we found nothing
        return -1;
    }
    

    【讨论】:

    • 代码按原样正常工作。可以通过当前深度以使整个事物尾递归,我没有这样做以更清晰地展示这个想法。
    【解决方案2】:

    我相信这样优雅的解决方案应该是遍历每一层的迭代代码。

    public int IterativeDepth(List<Object> lst, T element) 
    {
        // Set the first iteration
        int depth = 0;
        List<Object> next = new List<Object>();
    
        while ( ! IsNullOrEmpty(lst) ) 
        // For each layer
        {
            foreach (var current in lst) 
            // For each element of a layer
            {   
                if (current instanceof T && current == element)
                // Found it, return the depth
                {
                    return depth;
                }
    
                if (current instanceof List) {
                // It's a list, append it to the next iteration
                    next.Add(current);
                }
            }
    
            // Set the next iteration
            lst = next;
            next.clear();
            depth += 1;
        }
    
        // Found nothing
        return -1;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-22
      • 2016-07-18
      • 2022-11-01
      • 1970-01-01
      • 2011-01-12
      • 2015-02-17
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      相关资源
      最近更新 更多