Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created February 26, 2018 05:00
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/340019cc7c143b4903821ef0c93c080b to your computer and use it in GitHub Desktop.
Save jianminchen/340019cc7c143b4903821ef0c93c080b to your computer and use it in GitHub Desktop.
Spiral matrix - code review - ruby code - Feb. 25, 2018
def spiral_copy(inputMatrix)
# your code goes here
x_curr = 0
y_curr = 0
y_min = 0
y_max = inputMatrix[0].length - 1
x_min = 0
x_max = inputMatrix.length - 1
result = []
while x_min <= x_max and y_min <= y_max
x_curr = x_min
y_curr = y_min
while y_curr <= y_max
result.push(inputMatrix[x_curr][y_curr])
y_curr += 1
end
y_curr -= 1
x_curr += 1
while x_curr <= x_max
result.push(inputMatrix[x_curr][y_curr])
x_curr += 1
end
x_curr -= 1
y_curr -= 1
while y_curr >= y_min and x_min < x_max
result.push(inputMatrix[x_curr][y_curr])
y_curr -= 1
end
y_curr += 1
x_curr -= 1
while x_curr > x_min and y_min < y_max
result.push(inputMatrix[x_curr][y_curr])
x_curr -= 1
end
x_min += 1
x_max -= 1
y_min += 1
y_max -= 1
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment