From ef9b2aad95a12936f17d9c167bee5fc995955294 Mon Sep 17 00:00:00 2001 From: Yaseen Khan <78000116+Ykhan799@users.noreply.github.com> Date: Thu, 1 Sep 2022 18:15:00 -0700 Subject: [PATCH] Create: 48-Rotate-Image.swift --- swift/48-Rotate-Image.swift | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 swift/48-Rotate-Image.swift diff --git a/swift/48-Rotate-Image.swift b/swift/48-Rotate-Image.swift new file mode 100644 index 000000000..2434a34fe --- /dev/null +++ b/swift/48-Rotate-Image.swift @@ -0,0 +1,28 @@ +class Solution { + func rotate(_ matrix: inout [[Int]]) { + var left = 0, right = matrix.count - 1 + + while left < right { + for i in 0...right-left-1 { + let top = left, bottom = right + + // save the topLeft + let topLeft = matrix[top][left + i] + + // move bottom left into top left + matrix[top][left + i] = matrix[bottom - i][left] + + // move bottom right into bottom left + matrix[bottom - i][left] = matrix[bottom][right - i] + + // move top right into bottom right + matrix[bottom][right - i] = matrix[top + i][right] + + // move top left into top right + matrix[top + i][right] = topLeft + } + right -= 1 + left += 1 + } + } +} \ No newline at end of file