• 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
Copyright © 2020-2023  润新知