tags: C
Enter a matrix and print out each number in clockwise order from the outside to the inside. For example, the matrix is
,
The output result is [1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]
1. Obtain the row and col of the array, and use the loop to output one circle at a time to output all the data one by one.
Initialize circleNum=0, then the starting position of each circle is matrix[circleNum][circleNum], that is, the index of the starting position of the first circle in the array.
We can find the rule, the termination condition of the loop is col >circleNum*2 && row>circleNum*2
2. Draw a complete circle in the following four steps.
There are operating conditions for each step.Assuming that the coordinates of the last row of each circle are endY = row -1-circleNum;,
The coordinates of the last column of each circle are endX = col -1-circleNum analysis can get:
The first step will definitely run
The condition for running the second step is endY>circleNum, that is, the line number of the last line of the current circle is greater than the start line.
The condition of the third step is endY>circleNum && endX>circleNum, that is, the row number of the last row of the current circle must be greater than the start row, and the column number of the last column of the current circle must be greater than the column number of the start column.
The running condition of the fourth step is endY>circleNum+1 && endX>circleNum.
code show as below
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> res;
if(matrix.empty())
return res;
int circleNum = 0;
int row = matrix.size();
int col = matrix[0].size();
while(row>2*circleNum && col>2*circleNum) //circle draw a circle
{
oneCircle(matrix,res,row,col,circleNum);
circleNum++;
}
return res;
}
void oneCircle(vector<vector<int>> &matrix ,vector<int> &res,int row,int col,int circleNum)
{
int endX = col-1-circleNum; //The end column of each circle
int endY = row -1-circleNum; //The end row of each circle
for(int i = circleNum;i<=endX;i++) //The first step will definitely be carried out
res.push_back(matrix[circleNum][i]);
if(endY> circleNum) //Conditions for the second step
{
for(int i = circleNum+1;i<=endY;i++)
res.push_back(matrix[i][endX]);
}
if(endX> circleNum && endY >circleNum)//The third step
{
for(int i =endX-1;i>=circleNum;i--)
res.push_back(matrix[endY][i]);
}
if(endY >circleNum+1 && endX >circleNum)//Step 4
{
for(int i = endY-1;i>=circleNum+1;i--)
res.push_back(matrix[i][circleNum]);
}
}
Description of the topic Enter a matrix and print each number in a clockwise order from the outside to the inside, for example, if you enter the following 4 X 4 matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 1...
Description of the topic Enter a matrix and print each number in a clockwise order from the outside to the inside, for example, if you enter the following 4 X 4 matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 1...
An example of this matrix printed is as follows: code show as below: ...
Time limit: 1 second Space limitation: 32768K Given a number matrix, design an algorithm to print matrix elements clockwise from the top left corner Enter a description: The first line of input is two...
As the title, print a matrix clockwise. For example, the input matrix: 1 2 3 4 5 6 7 8 9 10...
Offer prove safety of the subject, an input matrix, each according to a number from outside to inside in a clockwise order of the print, such as: 1 2 3 4 5 &n...
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 seque...
Foreword Title: Enter a matrix, in accordance with each digital clockwise from outside to inside in order to print out sequentially, for example, if you enter the following matrix: 1 2 3,456,789,101,1...
Title: Enter a matrix, in order from outside to inside of a clockwise printed out successively for each digit. E.g: If the input bit matrix: 1 2 3 4 ...
29 interview questions: Clockwise Matrix Print An input matrix, each according to a number from the outside in order to print out sequentially clockwise. To expect to circle around the print, every la...