To prove safety Offer-19. Clockwise printing matrix (C ++ / Java)

topic:

Enter a matrix, in order from outside to inside in a clockwise order to print out sequentially for each number, for example, if you enter the following 4 X 4 matrix: 1,234,567,891,011,121,314 15 sequentially printed out 16 digital 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

analysis:

Clockwise from the outside of each digital print, is not considered a special case, in fact, every time the outer ring need to print up to four steps, from the starting point from left to right to print, and then from top to bottom, right to left, and finally and then print from bottom to top. And the starting coordinates of each print complete circle next time you print are the last elements in the lower right of the print starting point, consider a 6x6 matrix, starting with [0,0] position to start printing lap, next start then print from [1,1], then the [2,2], that is if you know the coordinates of the starting point, and the rectangular range you want to print, you can print clockwise.

However, according to the size of the rectangle, and sometimes it is not required to print the four steps. E.g:

 

 

This row n-column matrix, from left to right to print only once, but find that no matter what the situation, you need to print from left to right once.

When the second step if necessary to print the following:

 

 

That is, the number of lines is greater than 1, only need to print the second step.

The third step requires right-to-left printing situation is as follows:

 

 

Easy to see that the number of rows and number of columns is greater than 1 are required rectangle that will be required to print the third step.

And printing a fourth step for example as follows:

 

 

The number of columns is greater than 1, greater than the number of lines printed in claim 2. The fourth step will be required.

Each time you print digital outer ring, must determine whether you need to print 2,3,4 steps. Finally, write the exit condition of the loop, that is, no longer need to print a rectangle.

program:

C++

class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        if(matrix.empty()) return res;
        int m = matrix.size()-1;
        int n = matrix[0].size()-1;
        int i = 0;
        while((n-i) >= 0 && (m-i) >= 0){
            readHelper(matrix, i, i, m--, n--);
            i++;
        }
        return res;
    }
    void readHelper(vector<vector<int>> &v, int x, int y, int m, int n){
        //left to right
        int i = x;
        int j = y;
        for(; j <= n; ++j)
            res.push_back(v[i][j]);
        //up to down
        if(m-x > 0){
            for(i = x+1, j = n; i <= m; ++i)
                res.push_back(v[i][j]);
        }
        //right to left
        if(m-x > 0 && n - y > 0){
            for(j = n-1, i = m; j >= y; --j)
                res.push_back(v[i][j]);
        }
        //down to up
        if(m-x > 1 && n - y > 0){
            for(i = m-1, j = y; i > x; --i)
                res.push_back(v[i][j]);
        }
    }
private:
    vector<int> res;
};

Java

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
        res = new ArrayList<>();
        if( matrix.length == 0) return res;
        int m = matrix.length-1;
        int n = matrix[0].length-1;
        int index = 0;
        while(m-index >= 0 && n-index >= 0){
            helper(matrix, index, index, m--, n--);
            index++;
        }
        return res;
    }
    public void helper(int [][] matrix, int x, int y, int endX, int endY){
        for(int j = y; j <= endY; ++j)
            res.add(matrix[x][j]);
        if((endX - x) > 0){
            for(int i = x+1; i <= endX; ++i)
                res.add(matrix[i][endY]);
        }
        if((endX - x) > 0 && (endY - y) > 0){
            for(int j = endY-1; j >= y; --j)
                res.add(matrix[endX][j]);
        }
        if((endX - x) > 1 && (endY - y) > 0){
            for(int i = endX-1; i > x; --i)
                res.add(matrix[i][y]);
        }
    }
    private ArrayList<Integer> res;
}

 

Guess you like

Origin www.cnblogs.com/silentteller/p/11918473.html