【CodingBat】 linearIn问题 ★★★

问题描述:linearIn问题 ★★★

Given two arrays of ints sorted in increasing order, outer and inner, return true if all of the numbers in inner appear in outer. The best solution makes only a single "linear" pass of both arrays, taking advantage of the fact that both arrays are already in sorted order.


linearIn([1, 2, 4, 6], [2, 4]) → true
linearIn([1, 2, 4, 6], [2, 3, 4]) → false
linearIn([1, 2, 4, 4, 6], [2, 4]) → true

linearIn([1,2,3,4,5], [2,2,2,4])→true

参考别人的代码:

public boolean linearIn(int[] outer, int[] inner) {
  int i=0,j=0;
  while(iinner[i])
      return false;
    else
      i++;
  }
  if(i==inner.length) return true;
  return false;
}

笔记:这道题一开始没有想出好的解法,不知道如何利用好“两个数组都有序”的事实。看了别人的代码,觉得思路很值得学习。

你可能感兴趣的:(【CodingBat】 linearIn问题 ★★★)