赞
踩
Check whether an Array is Subarray of another Array(Visa)
Given two arrays A[] and B[] consisting of n and m integers. The task is to check whether the array B[] is a subarray of the array A[] or not.
Input : A[] = {2, 3, 0, 5, 1, 1, 2}, B[] = {3, 0, 5, 1}
Output : Yes
Input : A[] = {1, 2, 3, 4, 5}, B[] = {2, 5, 6}
Output : No
let solution = (a, b)=>{ // 当a[i] = b[j] 时 i++ j++,如果j=b.length 返回true // 当a[i] != b[j] 时 i=j-j+1 j=0 let [i,j] = [0,0] let [n,m] = [a.length,b.length] while(i < n && j < m){ if(a[i] == b[j]){ i++ j++ if(j == m){ return true } }else{ i = i - j + 1 j = 0 } } return false };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。