Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 22, 2018 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/b93800ea2300cc5c6c9e7b2c34eed29d to your computer and use it in GitHub Desktop.
Save jianminchen/b93800ea2300cc5c6c9e7b2c34eed29d to your computer and use it in GitHub Desktop.
spiral array copy - peer mock interview -
#include <iostream>
#include <vector>
using namespace std;
vector<int> spiralCopy( const vector<vector<int>>& inputMatrix )
{
// your code goes here
int x_length = inputMatrix[0].size(); // rows
int y_length = inputMatrix.size(); // columns
int matrix_size = x_length * y_length;
int count = 0;
vector<int> result;
// [[1]]
int x = 0, y = 0; // startX, startY
int startX = 0;
int startY = 0;
while (count < matrix_size)
{
startX = x; // remember horizontal starting point
// traverse right, horizontal
for(; x < x_length; x++)
{
cout <<
cout << "Inserting: " << inputMatrix[x][y] << endl;
result.push_back(inputMatrix[x][y]);
count++;
}
x--; // bring back last column
y++; // work on next row
startY = y; // remember vertical starting point
// traverse down
for (; y < y_length; y++) // x = ? , start value y should you go to last row, 10, 15, 20
{
cout << "Inserting: " << inputMatrix[x][y] << endl;
result.push_back(inputMatrix[x][y]);
count++;
}
y--; // bring back to last row
x--; // column next to last column
//traverse left
for (; x >= startX; x--) // startX
{
cout << "Inserting: " << inputMatrix[x][y] << endl;
result.push_back(inputMatrix[x][y]);
count++;
}
x++;
y--;
for (; y > startY; y--) // startY
{
cout << "Inserting: " << inputMatrix[x][y] << endl;
result.push_back(inputMatrix[x][y]);
count++;
}
x++;
y--;
x_length -= 2;
y_length -= 2;
}
return result;
}
int main() {
vector<vector<int>> inputMatrix = {{1}, {2}};
auto result = spiralCopy(inputMatrix);
//cout << "Size of result i"
for (int i : result)
cout << i << " ";
cout << endl;
return 0;
}
// x_length = horizontal length of array
// y_length = vertical length of array
// matrix_size = x_length * y_length
// every iteration, count+=1;
//traverse horizontal x_length elements
// traverse vertically down y_length-1
// traverse horizontally backwards x_length-1 elements
// traverse vertically up x_length-2
// x_length -= 1
// when count == matrix_size stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment