当前位置:网站首页>Lucky numbers in the matrix

Lucky numbers in the matrix

2022-06-26 14:09:00 fe11953264

subject

To give you one m * n Matrix , The number in the matrix Each are not identical . Please press arbitrarily Return all the lucky numbers in the matrix in order .

Lucky number refers to the elements in the matrix that meet the following two conditions at the same time :

The smallest of all elements in the same row
The largest of all elements in the same column

break And continue Comparison of

break You can only jump out of a loop . When there are multiple nested loops ,break Can only jump out of “ The parcel ” Its innermost layer of circulation , Can't jump out of all loops at once .
continue It's very simple , Its function is to end this cycle , That is, skip the following unexecuted statements in the loop body , Then, it is determined whether to execute the cycle next time .

Code

int* luckyNumbers (int** matrix, int matrixSize, int* matrixColSize, int* returnSize){
    int *adc=(int*)malloc(sizeof(int) * matrixSize * matrixColSize[0]);
    int shu = 0;
    for(int i = 0 ; i < matrixSize ; i++){
        for(int y = 0 ; y < matrixColSize[0] ; y++){
            bool max1 = true , min1 = true;
            for(int k = 0 ; k < matrixColSize[0] ; k++){           // The smallest... In the column 
                if(matrix[i][k] < matrix[i][y]){
                    min1 = false;
                    break;
                }
            }
            if(!min1){                                // Wrong column , Return to find a new value 
                continue;       
            }
            for(int n = 0 ; n < matrixSize ; n++){              // Maximum in row 
                if(matrix[n][y] > matrix[i][y]){
                    max1 = false;
                    break;
                }    
            }
            if(max1){
                adc[shu++] =matrix[i][y];
            }
            
        }
    }
    *returnSize = shu;
    return adc;
}
原网站

版权声明
本文为[fe11953264]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170510349634.html