Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归搜索数组-编码BAT_Java_Arrays_Algorithm_Recursion - Fatal编程技术网

Java 递归搜索数组-编码BAT

Java 递归搜索数组-编码BAT,java,arrays,algorithm,recursion,Java,Arrays,Algorithm,Recursion,为这个含糊不清的标题道歉,我想不出更具体的东西了 为了更好地递归解决问题,我一直在处理网站上发布的问题。我的问题与以下问题的一个变体有关 是: public boolean array220(int[] nums, int index) { if (index >= nums.length-1) return false; if (nums[index+1] == nums[index] * 10) return true; return array220(nums, +

为这个含糊不清的标题道歉,我想不出更具体的东西了

为了更好地递归解决问题,我一直在处理网站上发布的问题。我的问题与以下问题的一个变体有关

是:

public boolean array220(int[] nums, int index) {
  if (index >= nums.length-1) return false;

  if (nums[index+1] == nums[index] * 10) return true; 

  return array220(nums, ++index);
}
给定一个整数数组,如果该数组包含 数组中的某个值后跟该值乘以10。我们将 使用只考虑阵列中 从给定的索引开始。这样,递归调用就可以通过 索引+1可向下移动数组。初始调用将作为索引传入 0.

  • array220({1,2,20},0)→ 真的
  • array220({3,30},0)→ 真的
  • array220({3},0)→ 假的
我对这个问题的解决方案是:

public boolean array220(int[] nums, int index) {
  if (index >= nums.length-1) return false;

  if (nums[index+1] == nums[index] * 10) return true; 

  return array220(nums, ++index);
}
然而,为了挑战自己,我想知道我将如何着手解决我设想的这个问题的以下变化:

给定一个整数数组,如果数组包含 某个值比任何其他值大10倍。我们将使用 只考虑数组开始部分的约定 在给定的索引处。这样,递归调用可以将索引+1传递给 向下移动阵列。初始调用将作为0传入索引

例如:

  • 数组220({1,2,10,0)→ 真的
  • 数组220({3,2,9,38,20,0)→ 真的
  • array220({3},0)→ 假的
因此,基本上,与原始问题的区别在于,这些值不一定彼此相邻(参见上面的示例)

我将如何递归地进行此操作?我希望你能给我指点


我不想更改方法签名或使用全局变量

这就是答案,只要使用哈希集,并在进行递归调用时传递它:

public boolean array220(int[] nums,HashSet<Integer> set,  int index) {
  if (index >= nums.length-1) return false;

  if (set.contains(nums[index]*10)) 
      return true; 
  set.add(nums[index]);
  return array220(nums,set, ++index);
}
如果不想对数组进行排序,则使用线性递归搜索将给出O(n^2)解决方案

public boolean array220(int[] nums,  int index) {
  if (index >= nums.length-1) return false;

  if (linearSearch(0,nums[index]*10,nums)) 
      return true; 

  return array220(nums, ++index);
}

public boolean linearSearch(int start, int value, int[] nums){
     if(start >= nums.length)
        return false;

     if(nums[start] == value){
         return true;
     }else {
         return linearSearch(start + 1, value, nums);
     } 
}

这实际上不是一个递归问题,即使在它的第一个版本中也是如此。然而,迭代算法总是可以在尾部递归算法中转换,所以您可以只在递归算法中编写hastable解决方案fashion@DiciCodingBat上发布的问题只能通过递归来解决。我这样做是为了练习。我如何在不使用数据结构的情况下递归地执行此操作?看来我需要一种方法来执行嵌套循环。与您在原始版本中的操作类似,只需在recursive method中添加一个额外的哈希表作为参数。@sparrow此活动的全部目的是练习递归。编码BAT的问题被设计成无循环解决。请仔细阅读这个问题。效率或优雅与这个问题无关。@Phamtrong在不使用其他数据结构的情况下,我如何递归地执行此操作?你能提供一些伪代码吗?如果不使用数据结构,我们怎么做?我不关心效率。我不想更改方法签名或使用任何全局变量。@gentleArt,看看第二个和第三个解决方案,该解决方案不需要额外的数据结构。您能否澄清以下布尔条件:(nums[index]%10==0&&linearSearch(0,nums[index]/10,nums)),我不太确定这是在做什么?是否正在检查0?谢谢大家!@gentleArt
nums[index]%10==0
表示当前
nums[index]
是否除以10,例如:10,20,30,。。。如果是,那么我们需要找到1,2,3…,这是
nums[index]/10
@gentleArt-hmm,那部分不是必需的,只需删除那部分