I have tried same code for input
[ [1, 2, 3], [4, 5, 6]] but not getting correct output.
I am getting 4 and 5 interchanged but I the code is same
vector Solution::spiralOrder(const vector<vector > &A) {
int startRow = 0 ;
int startCol = 0 ;
int endRow = A.size()-1 ;
int endCol = A[0].size() - 1;
vector< int > ans ;
while(startRow <= endRow && startCol <= endCol){
//start row
for(int i = startCol ; i <= endCol; i++){
ans.push_back( A[startRow][i]) ;
}
startRow++ ;
//endcol
for(int i = startRow; i <= endRow; i++){
ans.push_back(A[i][endCol]);
}
endCol-- ;
//endrow
if(endRow > startRow){
for(int i = endCol; i >= startCol; i-- ){
ans.push_back(A[endRow][i]);
}
endRow-- ;
}
// startcol
if(endCol > startCol){
for(int i = endRow; i >= startRow; i-- ){
ans.push_back( A[i][startCol]) ;
}
startCol++ ;
}
}
return ans ;
}