leetcode刷题,总结,记录,备忘 344

leetcode344

Reverse String

  

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

Subscribe to see which companies asked this question

基础题,没啥说的。

class Solution {
public:
    string reverseString(string s) {
        if (s.size() == 0)
        {
            return s;
        }
        
        int m = s.size() / 2;
        for(int i = 0; i < m; ++ i)
        {
            char t = s[i];
            s[i] = s[s.size() - 1 - i];
            s[s.size() - 1 - i] = t;
        }
        
        return s;
    }
};


你可能感兴趣的:(leetcode刷题,总结,记录,备忘 344)