# 1460. Make Two Arrays Equal by Reversing Sub-arrays ###### tags: `Leetcode` `Easy` `FaceBook` Link: https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/ ## 思路 ### 思路一 排序 $O(NlogN)$ $O(1)$ ### 思路二 统计每个字符出现的次数并比较 $O(N)$ $O(N)$ 要记住这个one pass解法~ ## Code ### 思路一 ```java= class Solution { public boolean canBeEqual(int[] target, int[] arr) { Arrays.sort(target); Arrays.sort(arr); return Arrays.equals(target, arr); } } ``` ### 思路二 ```java= class Solution { public boolean canBeEqual(int[] target, int[] arr) { Map<Integer, Integer> count = new HashMap<>(); for(int i = 0;i < target.length;i++){ int tnum = target[i]; int anum = arr[i]; count.put(tnum, count.getOrDefault(tnum, 0)+1); count.put(anum, count.getOrDefault(anum, 0)-1); } for(int val:count.values()){ if(val!=0) return false; } return true; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up