# 0645. Set Mismatch ###### tags: `Leetcode` `Easy` `Indexing Sort` Link: https://leetcode.com/problems/set-mismatch/description/ ## 思路 和[0442. Find All Duplicates in an Array](https://hackmd.io/UnpI8ph4Q6icK2K1UxDHfg)差不多 ## Code ```java= class Solution { public int[] findErrorNums(int[] nums) { int[] ans = new int[2]; int n = nums.length; for(int i=0; i<n; i++){ if(nums[i]!=i+1){ while(nums[i]!=nums[nums[i]-1]){ swap(nums, i, nums[i]-1); } } } for(int i=0; i<n; i++){ if(nums[i]!=i+1){ ans[0] = nums[i]; ans[1] = i+1; } } return ans; } private void swap(int[] nums, int idx1, int idx2){ int temp = nums[idx1]; nums[idx1] = nums[idx2]; nums[idx2] = temp; } } ```
×
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