Re: [閒聊] 每日leetcode

看板Marginalman作者 (smart0eddie)時間1年前 (2024/08/03 22:13), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串630/1554 (看更多)
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
文章代碼(AID): #1chZhxOo (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1chZhxOo (Marginalman)