Skip to content

Instantly share code, notes, and snippets.

@mumunuu
Last active January 8, 2021 06:07
Show Gist options
  • Save mumunuu/4ba3e0c59e798d8f841101ebbc9e16ae to your computer and use it in GitHub Desktop.
Save mumunuu/4ba3e0c59e798d8f841101ebbc9e16ae to your computer and use it in GitHub Desktop.
algorithm(leetcode) Rotate Array
package main
func main() {
rotate([]int{1, 2, 3, 4, 5, 6, 7}, 3) //output [5,6,7,1,2,3,4]
rotate([]int{-1, -100, 3, 99}, 2) //output [3,99,-1,-100]
}
func rotate(nums []int, k int) {
temp := nums
for i := 1; i <= k; i++ { // k 번만큼 반복
lastIndex := len(temp) - 1
lastVal := temp[lastIndex] //마지막 숫자
temp = remove(temp, lastIndex)
temp = append([]int{lastVal}, temp...)
}
for i := 0; i < len(temp); i++ {
nums[i] = temp[i]
}
}
func remove(slice []int, s int) []int {
return append(slice[:s], slice[s+1:]...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment