leetcode344

  1. Reverse String
    Write a function that takes a string as input and returns the string reversed.
    Example:
    Given s = “hello”, return “olleh”.
    就是把一个字符串倒过来输出啦
class Solution {
public:
    string reverseString(string s) {
        int i = 0, j = s.size() - 1;
        while(i < j){
            swap(s[i++], s[j--]); 
        }
        return s;
    }
};

反正思路都一样~两头换一换

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

1、void swap (T& a, T& b):Exchanges the values of a and b.

你可能感兴趣的:(bj+string)