O(n) time O(n) space
跟242. Valid Anagram基本一样,只要两个数组能对的上就行,需要知道怎么reverse可以参考969. Pancake Sorting
1 2 3 4 5 6
| class Solution { public: bool canBeEqual(vector<int>& target, vector<int>& A) { return unordered_multiset<int>(A.begin(), A.end()) == unordered_multiset<int>(target.begin(),target.end()); } };
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public: bool canBeEqual(vector<int>& target, vector<int>& arr) { unordered_map<int, int> m; for (int x : target) { ++m[x]; } for (int x : arr) { if (--m[x] < 0) return false; } return true; } };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution { public: bool canBeEqual(vector<int>& target, vector<int>& arr) { unordered_map<int, int> m; for (int x : target) { ++m[x]; } for (int x : arr) { if (--m[x] == 0) { m.erase(x); } } return m.empty(); } };
|
1 2 3 4 5 6 7 8 9 10 11
| class Solution { public: bool canBeEqual(vector<int>& target, vector<int>& arr) { int m[1001] = {0}; for (int i = 0; i < target.size(); ++i) { ++m[target[i]]; --m[arr[i]]; } return all_of(begin(m), end(m), [](int x){ return x == 0; }); } };
|