본문 바로가기
LeetCode/Top Interview Q. - Easy

LeetCode 189. Rotate Array

by 벤진[Benzene] 2020. 5. 22.

Problem:

Given an array, rotate the array to the right by k steps, where k is non-negative.

Follow up:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3

Output: [5,6,7,1,2,3,4]

Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4]

 

Example 2:

Input: nums = [-1,-100,3,99], k = 2

Output: [3,99,-1,-100]

Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100]

 

-Summary-

-Brute Force-

1. We loop the len(k) times and pop the last element and insert into the beginning of the array.

 

Time Complexity: O(n*k)

Space Complexity: O(1)

 

모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]

https://leetcode.com/

'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글

LeetCode 48. Rotate Image  (0) 2020.05.22
LeetCode 283. Move Zeroes  (0) 2020.05.22
LeetCode 01. Two Sum  (0) 2020.05.21
LeetCode 36. Valid Sudoku [Medium]  (2) 2020.05.20
LeetCode 344. Reverse String [Easy]  (0) 2020.05.20

댓글