Created
November 18, 2017 23:11
-
-
Save jose-roberto-abreu/18b061ecca9fc427d19814475d1f70ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func rotateLeft(arr:[Int], d:Int)->[Int] { | |
if arr.count == 0 { | |
return arr | |
} | |
let distanceToMove = d % arr.count | |
var index = 0 | |
var tmpValue:Int? = arr[0] | |
var tmpArr = arr | |
for _ in 0..<tmpArr.count { | |
let tmpIndex = ((index - distanceToMove) < 0 ? arr.count + (index - distanceToMove) : index - distanceToMove) | |
if let value = tmpValue { | |
tmpValue = arr[tmpIndex] | |
tmpArr[tmpIndex] = value | |
} | |
index = tmpIndex | |
} | |
return tmpArr | |
} | |
var sample:[Int] = [1,2,3,4,5] | |
rotateLeft(arr: sample, d: 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment