Skip to content

Instantly share code, notes, and snippets.

@jose-roberto-abreu
Created November 18, 2017 23:11
Show Gist options
  • Save jose-roberto-abreu/18b061ecca9fc427d19814475d1f70ff to your computer and use it in GitHub Desktop.
Save jose-roberto-abreu/18b061ecca9fc427d19814475d1f70ff to your computer and use it in GitHub Desktop.
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