Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. package com.example
  2.  
  3. /**
  4. * https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem
  5. */
  6. class ArrayLeftRotation {
  7.  
  8. companion object {
  9.  
  10. fun leftRotate(list: List<Int>, times: Int): List<Int> {
  11. var rotated = list
  12. for (i in 0 until times) {
  13. rotated = singleLeftRotate(rotated)
  14. }
  15. return rotated
  16. }
  17.  
  18. private fun singleLeftRotate(list: List<Int>): List<Int> {
  19. val mutable = list.toMutableList()
  20. val temp = mutable[0]
  21. for (i in 1..mutable.lastIndex) {
  22. mutable[i - 1] = mutable[i]
  23. }
  24. mutable[mutable.lastIndex] = temp
  25. return mutable.toList()
  26. }
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement