一定要是一組數(shù)組完全包含另一組數(shù)組里的全部值,長度可以不同.一個值不同則不符合要求.
業(yè)精于勤,荒于嬉;行成于思,毀于隨。
SetA
and SetB
, assuming that the length of SetA
is less than or equal to SetB
, then SetA - SetB
code> should be an empty set to meet your requirements. Otherwise, even if it is not SetA
和SetB
, 假設(shè)SetA
的長度小于等于SetB
, 那么SetA - SetB
就應(yīng)該是空集, 才能滿足你的要求. 否則就算不是
C++標(biāo)準(zhǔn)庫有一個庫函數(shù)std::set_difference
std::set_difference
that can be used to calculate the difference of sets.PS: The two sets must be sorted??
There is a method in Java called list.containsAll()
public static void main(String args[]){
String[] a = {"01", "02", "03", "04","05"};
String[] b = {"01", "05", "03"};
System.out.println("isContains = " + isContains(a, b));
}
private static boolean isContains(String[] a,String[] b) {
List<String> listA = Arrays.asList(a);
List<String> listB = Arrays.asList(b);
return listA.containsAll(listB);
}
implementation of javascript
var p1=[77,9999,1];
var p2=[1,2,0,9999,4,65,77];
function isSubArray(p1,p2){
var i= 0,j= 0,resultArray=[];
p1.sort();
p2.sort();
if(p1[0]>p2[p2.length-1]||p1[p1.length-1]<p2[0]||p1.length>p2.length){
return false;
}
while(i<p1.length){
while(j<p2.length){
if(p1[i]>p2[j]){
j++;
}else if(p1[i]<p2[j]){
i++;
break;
}else{
resultArray.push(p1[i]);
i++;
j++;
}
}
if(j==p2.length){
break;
}
}
return resultArray.length===p1.length;
}
!isSubArray(p1,p2)?console.log('p1 isn\'t subset of p2'):console.log('p1 is subset of p2');
Find the longest array first, then loop the short array and determine whether the element is in the long array
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
String[] strArr1={"ee","aa","bb","cc"};
String[] strArr2={"aa","bb","cc","dd","44"};
System.out.println(Main.subContain(strArr1,strArr2));
}
public static boolean subContain(String[] strArr1, String[] strArr2) {
if (null == strArr1 || null == strArr2) {
return false;
}
boolean flag = true;
if (strArr2.length > strArr1.length) {
String[] temp;
temp = strArr1;
strArr1 = strArr2;
strArr2 = temp;
}
for (String str : strArr2) {
if (!Arrays.asList(strArr1).contains(str)) {
flag = false;
break;
}
}
return flag;
}
}
To determine whether the intersection length of two arrays is equal to the minimum array length, a third-party jar package is used
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
String[] strArr1 = {"aa", "bb", "cc"};
String[] strArr2 = {"aa", "bb", "cc", "dd", "44"};
System.out.println(Main.subContain(strArr1, strArr2));
}
public static boolean subContain(String[] strArr1, String[] strArr2) {
if (null == strArr1 || null == strArr2) {
return false;
}
Collection collection = org.apache.commons.collections.CollectionUtils.intersection(Arrays.asList(strArr1), Arrays.asList(strArr2));
return collection.size() == Math.min(strArr1.length, strArr2.length);
}
}