Summarize:
- The key point is the analysis law, can analyze the simple concrete example, attempts to seek the universal law.
- The use of array random access and continuous storage features, convenient operation.
- Pay special attention to orderly or disordered state.
1 Public classSolution {2 /*face question 3:3 all numbers in an array of length n are within the range of 0 to n-1. Some of the numbers in the array are duplicates,4 but I don't know how many numbers are duplicated. I don't know how many times each number repeats. Please find any duplicate numbers in the array. 5 For example, if you enter an array of length 7 {2,3,1,0,2,5,3}, the corresponding output is the first repeating number 2. 6 */7 //scan from the beginning to determine if the number and subscript are in compliance, then do not process, continue to judge the next number8 //non-conformance is to determine whether the current number and its corresponding position of the number is equal, if it is equal to find duplicate numbers, that is, the current number9 //if not equal, the position of the exchange of numbers, continue to judge the current position of the number, note is to continue the current position of the judgment while control, in line with the next round for loopTen Public BooleanDuplicateintNumber[],intLengthint[] DUP) { One inttemp; A if(Length <=1)return false; - for(inti = 0;i <number.length;i++){ - while(Number[i]! =i) {//The current number is not equal to the previous subscript, the number that is equal to the position of the digit subscript the if(Number[number[i]]! =Number[i]) {//If not equal, the interchange -temp =Number[number[i]]; -Number[number[i]] =Number[i]; -Number[i] =temp; + } - Else{//If equal, indicates a duplicate number is found, returns and exits +Dup[0] =Number[i]; A return true; at } - } - } - return false; - } - /*face question 4: in in a two-dimensional array, each row is ordered in ascending order from left to right . - Each column is sorted in ascending order from top to bottom. Please complete a function, to enter such a two-dimensional array and an integer to determine if the array contains the integer. + */ - /* the idea: The key is to find the law, starting from the upper right corner to judge, gradually remove the row or column (change to the line number or column number of simple operation) * */ $ Public BooleanFind (intTargetint[] Array) {Panax Notoginseng inti = 0,len = Array.Length;//Number of rows - intj = Array[0].length-1; the while(I < len && J >= 0){ + if(target = = Array[i][j])return true; A Else if(Target < array[i][j]) j--; the Elsei++; + } - return false; $ } $ Public Static voidMain (String args[]) { - intNum[] = {2,3,1,0,2,5,3},dup[]={0}; - intA[][] = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}}; theSolution s =Newsolution (); -Boolean flag =s.duplicate (num, num.length,dup);Wuyi if(flag) { theSystem.out.println (dup[0]); - } WuSystem.out.println (S.find (10, a)); - } About}
An array of points of offer