Array Left Rotation

package arrays

/**
 * https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem
 *
 * A left rotation operation on an array shifts each of the array's elements  unit to the left.
 * For example, if  left rotations are performed on array , then the array would become .
Given an array  of  integers and a number, perform  left rotations on the array.
Return the updated array to be printed as a single line of space-separated integers.
 * */
class LeftRotation {
    fun rotLeft(a: Array<Int>, d: Int): Array<Int> {
        val size = a.size
        val result = Array<Int>(size, { 0 })
        //get the start point to rotated
        val mod = d % size
        for (i in a.indices) {
            val index = (i + mod) % size
            result.set(i, a[index])
        }
        return result
    }
}
原文地址:https://www.cnblogs.com/johnnyzhao/p/13187533.html