Swift Solution for Rotate Image


  • 0
    A
    class Solution {
        
        /// Rotate a matrix 90 degrees counter clockwise, do it in-place
        /// with no extra storage except for constant variable space
        func rotate(_ matrix: inout [[Int]]) {
    
            var currentRow = -1
            var currentCol = -1
            var distance = (matrix.count - 1) 
            var currentIteration = 1
    
            //Rotate matrix starting from outter border/square to inner.
            //Essentially think of rotating concentric squares.
            while currentIteration <= matrix.count / 2 {
    
                //Move start position to inner square
                currentRow += 1
                currentCol += 1
                
                //Reduce the distance at each iteration by 2
                //You are rotating smaller and smaller inner squares
                if (currentIteration > 1) {
                    distance -= 2
                }
    
                //NOTE: The number of variables used in this program can
                //be significantly reduced if we truly are concerned about space
                //(i.e. at a constrained embedded system level).
                //However, keeping as is for clarity of the program and readability.
    
                //Compute new rotation boundaries for new square
                let rightColumnBoundary = currentCol + distance
                let leftColumnBoundary = currentCol
                let topRowBoundary = currentRow
                let bottomRowBoundary = currentRow + distance
    
                //Begin rotation for square
                for i in 0..<distance {
                    var swapRow = currentRow
                    var swapCol = currentCol + i
                    var swapValue = matrix[swapRow][swapCol]
    
                    //Make a total of 4 swaps at each position (90 degrees)
                    for numSwaps in 0..<4 {
                        if numSwaps == 0 {
                            let distanceToTraverse = swapCol - leftColumnBoundary
                            swapCol = rightColumnBoundary
                            swapRow = swapRow + distanceToTraverse
                        } else if numSwaps == 1 {
                            let distanceToTraverse = swapRow - topRowBoundary
                            swapCol = swapCol - distanceToTraverse
                            swapRow = bottomRowBoundary
                        } else if numSwaps == 2 {
                            let distanceToTraverse = rightColumnBoundary - swapCol
                            swapCol = leftColumnBoundary
                            swapRow = swapRow - distanceToTraverse
                        } else {
                            let distanceToTraverse = bottomRowBoundary - swapRow
                            swapCol = swapCol + distanceToTraverse
                            swapRow = topRowBoundary
                        }
    
                        let tempValue = matrix[swapRow][swapCol]
                        matrix[swapRow][swapCol] = swapValue
                        swapValue = tempValue
                    }
                }
    
                currentIteration += 1
            }
    
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.