【双指针】344. 反转字符串

344. 反转字符串

解题思路

  • 设置双指针
  • 设置前后指针,前后指针进行比较 交换字符
class Solution {
    public void reverseString(char[] s) {
        // 原地修改字符串 不可以设置新的数组
        // 设置双指针
        // 设置前后指针
        int first = 0;
        int tail = s.length - 1;

        while(first < tail){
            char temp = s[first];
            s[first] = s[tail];
            s[tail] = temp;

            first++;
            tail--;
        }

    }
}

你可能感兴趣的:(#,Leetcode,算法,数据结构)