Question Name: Cyclic-Rotation or CyclicRotation
With Python’s syntactic sugar, the solution is pretty short.
def solution(A, K):
# write your code in Python 2.7
if len(A) == 0:
K = K % len(A)
return A[-K:] + A[:-K]
Well, kind of too easy.
Let’s try a C++ solution to demo the details better.
Thanks to @micropentium6, the original C++ solution is ugly and bad. Please refer to the solution from @micropentium6. Also another Python solution with the same method as @micropentium6’s can be found in an older post.