[23333333] summary of 2 impressive solution


  • 1
    2

    Solution 1

       class Solution {
        public:
            void rotate(vector<int>& nums, int k) {
                int size_nums = nums.size();
                k = k%size_nums;
                reverse(nums.begin(), nums.begin() + size_nums - k);
                reverse(nums.begin() + size_nums - k, nums.end());
                reverse(nums.begin(), nums.end());
                return;
            }
        };
    
    
    Solution 2
    
    class Solution {
    public:
        void rotate(vector<int>& nums, int k) {
            int n = nums.size();
            int start = 0;
            for(; k %= n; n -= k, start += k) {
                for(int i = 0; i < k; i++) {
                    swap(nums[start+i], nums[start+n-k+i]);
                }
            }
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.