精華區beta Marginalman 關於我們 聯絡資訊
2024-08-02 1460. Make Two Arrays Equal by Reversing Subarrays You are given two integer arrays of equal length target and arr. In one step, you can select any non-empty subarray of arr and reverse it. You are allowed to make any number of steps. Return true if you can make arr equal to target or false otherwise. 因為反轉不限次數 不限長度 所以實際上等同可以任意調整位置 那只要兩個有相同的元素就一定可以調出來 反正我們不用真的調出來 所以只需要比對兩個array的元素是不是都一樣 class Solution { public: bool canBeEqual(vector<int>& target, vector<int>& arr) { vector<int> checkcount(1001); for (int i = 0; i < target.size(); ++i) { checkcount[target[i]]++; checkcount[arr[i]]--; } for (int i = 0; i < checkcount.size(); ++i) { if (checkcount[i]) { return false; } } return true; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 73.173.211.221 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1722694395.A.632.html