An array of issues (Java implementation)

<A> Find a two-dimensional array

Title Description

(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.
code show as below:
public class Solution {
    public boolean Find(int target, int [][] array) {
        
        for(int i = 0;i < array.length;i++){
            for(int j = array[0].length - 1;j >=0;j--){
                 if(array[i][j] > target){  //取值和目标整数相比较!
                    j--; 
                 }else if(array[i][j] < target){
                    i++;
                 }else
                    return true;
            }
        }
        return false;
    }
}

 Pass test method described above, it may be the cause of the time complexity.

Code is as follows :( perfect start from the bottom left corner of the matrix)
{Solution class public 
    public Boolean the Find (target int, int [] [] Array) { 

        int len be array.length = -. 1; // row 
        int i = 0; // column 
        while (len> = 0 && i <array [ 0] .length) {// row is greater than zero, the column is less than the total length of the array 
            if (array [len] [i ]> target) {// target values and the integer comparison! 
                len--; 
            } the else IF (Array [len] [I] <target) { 
                I ++; 
            } the else 
                return to true; 
        } 
        return to false; 
    } 
}

 Code is as follows :( perfect start from the top right corner of the matrix)

{Solution class public 
    public Boolean the Find (target int, int [] [] Array) { 

        int len = 0; // row 
        int i = array [0] .length - 1; // row 
        while (len <array.length && i> = 0) {// row is greater than zero, the column is less than the total length of the array 
            if (array [len] [i ]> target) {// target values and the integer comparison! 
                i--; 
            } the else IF (Array [len] [I] <target) { 
                len ++; 
            } the else 
                return to true; 
        } 
        return to false; 
    } 
}

 <Two> array duplicate numbers

Title Description

All numbers in a length of n in the array are in the range 0 to n-1. Some digital array is duplicated, but do not know how many numbers are duplicated. Do not know each digit is repeated several times. Please find an array of any one of the duplicate numbers. For example, if the length of the input array 7 {2,3,1,0,2,5,3}, then the corresponding output of the first 2 repeating digits.
code show as below:
import java.util.HashSet;
public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个,赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
    
        if(length <= 0){
            return false;
        }
        HashSet<Integer> se = new HashSet<>();
        for(int i = 0; i < length;i++){
            if(se.contains(numbers[i])){
                duplication[0] = numbers[i];
                return true;
            }else{
                se.add(numbers[i]);
            }
        }
       return false;
    }
}

<Three> Construction product array

Title Description

Given an array A [0,1, ..., n-1], please construct an array B [0,1, ..., n-1], where B is the element B [i] = A [ 0] * A [1] * ... * A [i-1] * A [i + 1] * ... * A [n-1]. You can not use the division.
import java.util.ArrayList;
public class Solution {
    public int[] multiply(int[] A) {

        int len = A.length;
        int[] B = new int[len];
        
        int res = 1;
        for(int i = 0;i < len;res *= A[i++]){
            B[i] = res;
        }
        res = 1;
        for(int j = len - 1;j >= 0;res *= A[j--]){
            B[j] *= res;
        }
        return B;
    }
}

 

 

 

Guess you like

Origin www.cnblogs.com/youdiaodaxue16/p/11359136.html