Java: A Bitmap beats %91, very simple


  • 0
    T
    public class Solution {
        public int[] findErrorNums(int[] nums) {
            int len = nums.length;
            boolean[] bitmap = new boolean[len];
            int[] results = new int[2];
            
            for ( int i = 0; i < len; i++ ) {
                int item = nums[i];
                if (bitmap[item - 1]) {
                    results[0] = item;
                } else {
                    bitmap[item - 1] = true;
                }
            }
            
            for ( int i = 0; i < len; i++ ) {
                if (!bitmap[i]) {
                    results[1] = i+1;
                }
            }
            
            return results;
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.