leetcode-344-反转字符串

问题:

package com.example.demo;

public class Test344 {
    public void reverseString(char[] s) {
        // 双指针
        int left = 0;
        int right = s.length - 1;
        while (left < right) {
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
    }

    public static void main(String[] args) {
        Test344 t = new Test344();
        char[] arr = {'a', 'b', 'c'};
        t.reverseString(arr);
        for (char c : arr) {
            System.out.println(c);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/nxzblogs/p/11271598.html