# Leetcode 645. Set Mismatch # Math.abs() Runtime: 3 ms faster than 60.19% 找重複數=> 遍歷每個數,將其對應的位置上的數變為負數,這樣如果我們再變其為負數之前已經是負數了,說明該數重複 找缺少的數=> 遍歷原數組,如果某個位置上的數為正數,說明該位置對應的數沒有出現過 ``` java= class Solution { public int[] findErrorNums(int[] nums) { int[] result=new int[2]; for(int num : nums){ if(nums[Math.abs(num)-1]<0) result[0]=Math.abs(num); else nums[Math.abs(num)-1]*=-1; } for(int i=0;i<nums.length;i++){ if(nums[i]>0){ result[1]=i+1; break; } } return result; } } ``` --- ###### tags: `待改進` `Array`
×
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