03. array face questions duplicate numbers

class Solution {
     public  int findRepeatNumber ( int [] the nums) {
         // iterate 
        for ( int I = 0; I <nums.length; I ++ ) {
             // if array element is not equal to the subscript (sort operation is performed) 
            the while (the nums [I]! = I) {
                 // If the position of the element with his index corresponding to this element represented equal, it is found that repeating element 
                IF (the nums [I] == the nums [the nums [I]]) return the nums [I];
                 // him with this element represented by the subscript position corresponding switching element 
                int tmp = the nums [I]; 
                the nums [I] = the nums [tmp]; 
                the nums [tmp] = tmp;
            }
        }
        return 0;
    }
}

Digital array are in the range of 0 ~ n-1. If the array elements is not repeated, then drained after the array sequence, subscript number i i appears at the next. Some locations may exist more digits, while some locations may not have numbers.

Through the array, when scanned digital subscript i is compared first figure (denoted by m) is not equal to i.

If yes, then scan the next number; if not, then let his numbers m and compared. If the m-th digit and he equal, to find a repeating digits; and if he is not equal to the m-th digit, the i-th digit and put the m-th digital switching

Then repeat this comparison and exchange process, until we find a duplicate numbers.

Guess you like

Origin www.cnblogs.com/hzqshuai/p/12304802.html