面試OR筆試11 集合交集

2021-08-07 10:47:04 字數 1085 閱讀 6722

求兩個陣列的元素交集。

#include#include#include#include#includeusing namespace std;

void vprint(vector&v)

// 方法1: 直接呼叫stl中的set運算函式

// o( (m+n)*lgm )

vectorsetintersection1(const vector&a, const vector&b)

// 方法2: 乙個排序, 然後二分查詢 (stl實現)

// 排序 o(mlgm), 查詢 o(nlgm), 綜合 o( (m+n)*lgm )

vectorsetintersection2(vector&a, vector&b)

if (a[low] == b) vr.push_back(b);

} return vr;

}// 方法3: 先排序兩個陣列, 再用迭代器遍歷, 相等輸出, 較小的捨棄

// 排序 max(o(nlgn),o(mlgm)), 查詢 o(m+n), 綜合: max(o(nlgn),o(mlgm))

vectorsetintersection3(vector&a, vector&b)

else if (*ita<*itb) ++ita;

else ++itb;

} return vr;

}// 方法4: o(n*m)的方法,直接比較資料然後輸出

vectorsetintersection4(vector&a, vector&b)

return vr;

}int main()

; vectorb = ;

vprint(setintersection1(a, b)); cout << endl;

vprint(setintersection2(a, b)); cout << endl;

vprint(setintersection3(a, b)); cout << endl;

vprint(setintersection4(a, b)); cout << endl;

return 0;

}

集合11 集合 Collections工具類

collections 操作collection map的工具類 arraylist list new arraylist list.add 123 list.add 456 list.add 456 list.add 89 list.add 23 system.out.println list 1...

python3集合 Python3 集合

集合 set 是乙個無序的不重複元素序列。可以使用大括號 或者 set 函式建立集合,注意 建立乙個空集合必須用 set 而不是 因為 是用來建立乙個空字典。建立格式 parame 或者set value 這裡演示的是去重功能 orange in basket 快速判斷元素是否在集合內 true c...

python3集合 Python3 集合

python3 集合 集合 set 是乙個無序的不重複元素序列。可以使用大括號或者set 函式建立集合,注意 建立乙個空集合必須用set 而不是,因為是用來建立乙個空字典。集合內建方法 add 為集合新增元素 例項 fruits.add orange print fruits 輸出結果為 clear...