Three times reverse(C++)


  • 3
    B
    class Solution {
    public:
    void rotate(vector<int>& nums, int k) {
        if( k == 0 || k ==nums.size())
            return;
        else{
            k = k%(nums.size());
            reverse(nums.begin(),nums.end());
            reverse(nums.begin(),nums.begin() + k);
            reverse(nums.begin()+k,nums.end());
        }
      }
    };

  • 0
    D

    you do not need to do anything if k= N times of nums.size().


Log in to reply
 

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