jeudi 18 février 2021

C++ Same Code, different output how to fix?

why aren't both codes behaving the same? I only moved /2 from one line to the other.

class Solution {
public:
    void reverseString(vector<char>& s) {
        int n=s.size()/2;
        for (int i=0;i<n;++i)
        {
            std::swap(s[i],s[n-i-1]);
        }
    }
};

Other Code:

class Solution {
public:
    void reverseString(vector<char>& s) {
        int n=s.size();
        for (int i=0;i<n/2;++i)
        {
            std::swap(s[i],s[n-i-1]);
        }
    }
};

Aucun commentaire:

Enregistrer un commentaire