Spiral Matrix:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
You should return [1,2,3,6,9,8,7,4,5]
.
一圈一圈的加,注意下标就好了。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &mat) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(mat.empty())
return vector<int>();
int height=mat.size(),width=mat[0].size();
int x=0;
vector<int> seq;
while(height>1&&width>1)
{
for(int i=0;i<width-1;i++)
seq.push_back(mat[x][x+i]);
for(int i=0;i<height-1;i++)
seq.push_back(mat[x+i][x+width-1]);
for(int i=0;i<width-1;i++)
seq.push_back(mat[x+height-1][x+width-1-i]);
for(int i=0;i<height-1;i++)
seq.push_back(mat[x+height-1-i][x]);
x+=1;
width-=2;
height-=2;
}
if(height==1)
for(int i=0;i<width;i++)
seq.push_back(mat[x][x+i]);
else if(width==1)
for(int i=0;i<height;i++)
seq.push_back(mat[x+i][x]);
return seq;
}
};
Rotate Image:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
class Solution {
public:
void rotate(vector<vector<int> > &mat) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n=mat.size();
int stx=0,sty=0;
int w=n,h=n;
while(w>=1&&h>=1)
{
for(int i=0;i<w-1;i++)
{
int tmp=mat[stx][sty+i];
swap(mat[stx+h-1-i][sty],mat[stx][sty+i]);
swap(mat[stx+h-1][sty+w-1-i],mat[stx+h-1-i][sty]);
swap(mat[stx+i][sty+w-1],mat[stx+h-1][sty+w-1-i]);
mat[stx+i][sty+w-1]=tmp;
}
stx+=1;
sty+=1;
w-=2;
h-=2;
}
}
};
Spiral Matrix II:
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]跟第一个是一样的,我把一圈圈遍历时每次的范围改了下,这回要清楚一些。
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
// assert(n>=0);
if ( n==0 )
return vector<vector<int> >();
vector<vector<int> > mat(n,vector<int>(n,0));
int x=0,y=0;
int width=n;
int k=1;
while(width>0)
{
if ( width==1 )
{
mat[x][y]=k++;
break;
}
int tWidth=width-1;
for(int i=0;i<tWidth;i++)
mat[x][y+i]=k++;
for(int i=0;i<tWidth;i++)
mat[x+i][y+width-1]=k++;
for(int i=0;i<tWidth;i++)
mat[x+width-1][y+width-1-i]=k++;
for(int i=0;i<tWidth;i++)
mat[x+width-1-i][y]=k++;
width-=2;
x++,y++;
}
return mat;
}
};
Set Matrix Zeroes:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n)
space, but still not the best solution.
Could you devise a constant space solution?
class Solution {
public:
void setZeroes(vector<vector<int> > &mat) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m=mat.size();
int n=mat[0].size();
bool setCol=false,setRow=false;
if ( mat[0][0]==0 )
{
setCol=setRow=true;
}
else
{
for(int i=0;i<m;i++)
if ( mat[i][0]==0 )
setCol=true;
for(int j=0;j<n;j++)
if ( mat[0][j]==0 )
setRow=true;
}
for(int i=1;i<m;i++)
for(int j=1;j<n;j++)
{
if ( mat[i][j]==0 )
mat[i][0]=mat[0][j]=0;
}
for(int i=1;i<m;i++)
for(int j=1;j<n;j++)
if (mat[i][0]==0||mat[0][j]==0)
mat[i][j]=0;
if ( setRow )
for(int j=0;j<n;j++)
mat[0][j]=0;
if ( setCol )
for(int i=0;i<m;i++)
mat[i][0]=0;
}
};
Search a 2D Matrix:
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ]
Given target = 3
,
return true
.
class Solution {
public:
bool searchMatrix(vector<vector<int> > &mat, int t) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m=mat.size();
if ( m==0 )
return false;
int n= mat[0].size();
int x=0,y=n-1;
while(x<m&&y>=0)
{
if ( mat[x][y]==t)
return true;
else if ( mat[x][y]<t )
x++;
else
y--;
}
return false;
}
};