leetcode系列344-反转字符串

    技术2025-02-04  5

    【题目概要】

    344. Reverse String Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. You may assume all the characters consist of printable ascii characters. Example 1: Input: ["h","e","l","l","o"] Output: ["o","l","l","e","h"]

    【思路分析】

    注意要求是在原地修改数组,只能使用O(1)的内存空间,双指针循环交换字符

    【代码示例】

    void reverseString(char* s, int sSize){ int left = 0; int right = sSize-1; while(left < right) { char temp = s[left]; s[left] = s[right]; s[right] = temp; left++; right--; } }
    Processed: 0.009, SQL: 9