What is problem of my code


  • 0
    Z
    class Solution {
    void exchange(vector<int>& nums, int begin, int end)
    {
        while(begin<end)
        {
            if(nums[begin]==nums[end])return;
            nums[begin] = nums[begin]^nums[end];
            nums[end] = nums[begin]^nums[end];
            nums[begin] = nums[begin]^nums[end];
            ++begin,--end;
            
        }
    }
     public:
       void rotate(vector<int>& nums, int k) {
          if(nums.size()<=1)return ;
          if(k<=0)return;
          k%=nums.size();
          if(k==0)return;
          exchange(nums,0,nums.size()-k-1);
          exchange(nums,nums.size()-k,nums.size()-1);
          exchange(nums,0,nums.size()-1);
      }
    

    };


  • 0

    if(nums[begin]==nums[end])return; makes no sense and causes the problem.


  • 0
    Z

    I add that bcz :

        nums[begin] = nums[begin]^nums[end]; is 0x00, if they are same..
    

    so both the two value are changed..


  • 0

    No idea what you're trying to say. Looks like you're trying to avoid a problem that doesn't exist, and create a real problem along the way.


Log in to reply
 

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