Likou 344. Reverse string-double pointer

344. Reverse String

Write a function whose role is to reverse the input string. The input string is given in the form of a character array char[].

Don't allocate extra space to another array, you must modify the input array in place and use O(1) extra space to solve this problem.

You can assume that all characters in the array are printable characters in the ASCII code table.

Example 1:

Input: ["h", "e", "l", "l", "o"]
Output: ["o", "l", "l", "e", "h"]
Example 2:

Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a", "H"]

void reverseString(char* s, int sSize){
    
    
    char temp;
    int i = 0;
    int j = sSize-1;
    while (i <= j) {
    
    
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
        i++;
        j--;
    }
}

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/112547635