less than 1 minute read

Tags: ,

Arrays Left rotation

哈哈哈 這題之前在 codility 有玩過~ python 速解!

import collections
# Complete the rotLeft function below.
def rotLeft(a, d):
    qu = collections.deque(a)
    qu.rotate(-d)
    return qu
  • javascript

    • 現學現賣,這個執行時間 應該是 O(n) XDD 應該吧….

    • Imgur

// Complete the rotLeft function below.
function rotLeft(a, d) {
    let leading_arr = []
    let rotated_arr = []
    for ( let i = 0; i < a.length; i++ ) {
        // leading
        if (i < d) {
           leading_arr.push(a[i])
        }
        else if /** rear_ar **/ (i >= d) {
          rotated_arr.push(a[i])
        }
    }
    rotated_arr.push(...leading_arr)
    return rotated_arr
}