From 35030f36952a295deb153331c569973a5ffa764d Mon Sep 17 00:00:00 2001 From: Shambhavi Sharma <60618802+ShambhaviSharma0110@users.noreply.github.com> Date: Sun, 16 May 2021 22:05:08 +0530 Subject: [PATCH 1/5] added 189 --- C++/189.Rotate array | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 C++/189.Rotate array diff --git a/C++/189.Rotate array b/C++/189.Rotate array new file mode 100644 index 00000000..3630b13e --- /dev/null +++ b/C++/189.Rotate array @@ -0,0 +1,28 @@ +class Solution { +public: + void rotate(vector& nums, int k) { + int n = nums.size(); + k = k % n; + // we first reverse all the elements,[7 6 5 4 3 2 1] + for (int i = 0, j = n - 1; i < j; i++, j--) { + swap(nums[i], nums[j]); + } + // then we reverse the set of elements sized k for example [7 6 5 4 3 2 1] = [ 5 6 7 4 3 2 1] + for (int i = 0, j = k - 1; i < j; i++, j--) { + swap(nums[i], nums[j]); + } + //finally we reverse the ending elements too = 5 6 7 1 2 3 4 + for (int i = k, j = n - 1; i < j; i++, j--) { + swap(nums[i], nums[j]); + } + } +}; +/* +Input: nums = [1,2,3,4,5,6,7], k = 3 +Output: [5,6,7,1,2,3,4] + +Example 2: + +Input: nums = [-1,-100,3,99], k = 2 +Output: [3,99,-1,-100] +*/ From 53219fb53d94793eb840862f0484139f044ff708 Mon Sep 17 00:00:00 2001 From: Shambhavi Sharma <60618802+ShambhaviSharma0110@users.noreply.github.com> Date: Sun, 16 May 2021 22:10:22 +0530 Subject: [PATCH 2/5] added 189 --- C++/189.Rotate array | 3 +++ 1 file changed, 3 insertions(+) diff --git a/C++/189.Rotate array b/C++/189.Rotate array index 3630b13e..9da5258c 100644 --- a/C++/189.Rotate array +++ b/C++/189.Rotate array @@ -17,6 +17,9 @@ public: } } }; + +Time complexity:O(n) +Space Complexity:O(1) /* Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] From f4f33fef86a9ae8d4fda9f2267d224a791170ce2 Mon Sep 17 00:00:00 2001 From: Shambhavi Sharma <60618802+ShambhaviSharma0110@users.noreply.github.com> Date: Mon, 17 May 2021 11:28:21 +0530 Subject: [PATCH 3/5] Update 189.Rotate array --- C++/189.Rotate array | 1 + 1 file changed, 1 insertion(+) diff --git a/C++/189.Rotate array b/C++/189.Rotate array index 9da5258c..890f5eb0 100644 --- a/C++/189.Rotate array +++ b/C++/189.Rotate array @@ -1,3 +1,4 @@ +difficulty level: Easy class Solution { public: void rotate(vector& nums, int k) { From bbff35623c22e843c61fc23e423ba05e28a5cec8 Mon Sep 17 00:00:00 2001 From: Shambhavi Sharma <60618802+ShambhaviSharma0110@users.noreply.github.com> Date: Mon, 17 May 2021 11:33:47 +0530 Subject: [PATCH 4/5] added 189 --- C++/{189.Rotate array => 189.Rotate-array} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename C++/{189.Rotate array => 189.Rotate-array} (97%) diff --git a/C++/189.Rotate array b/C++/189.Rotate-array similarity index 97% rename from C++/189.Rotate array rename to C++/189.Rotate-array index 890f5eb0..faed77b3 100644 --- a/C++/189.Rotate array +++ b/C++/189.Rotate-array @@ -1,4 +1,4 @@ -difficulty level: Easy +difficulty level: Medium class Solution { public: void rotate(vector& nums, int k) { From 72a3c2eaef8aa213c66f72b5b8e6a165d98dd0e5 Mon Sep 17 00:00:00 2001 From: Shambhavi Sharma <60618802+ShambhaviSharma0110@users.noreply.github.com> Date: Mon, 17 May 2021 11:34:40 +0530 Subject: [PATCH 5/5] added 189 --- C++/{189.Rotate-array => 189.Rotate-array.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename C++/{189.Rotate-array => 189.Rotate-array.cpp} (100%) diff --git a/C++/189.Rotate-array b/C++/189.Rotate-array.cpp similarity index 100% rename from C++/189.Rotate-array rename to C++/189.Rotate-array.cpp