leetcode. -Java reverse text string .344

1. Specific topics

Write a function, its role is to input string reversed. Input string to a character array char [] given in the form. Do not allocate additional space to another array, you must place modify the input array, use the extra space O (1) solution to this problem. You may assume that all the characters in the array are the ASCII table printable characters.

 示例 1:输入:["h","e","l","l","o"]  输出:["o","l","l","e","h"]

示例 2:输入:["H","a","n","n","a","h"]  输出:["h","a","n","n","a","H"]

2. Analysis of ideas

Double pointer, inclusive exchange

3. Code

 1 public void reverseString(char[] s) {
 2         int left = 0;
 3         int right = s.length - 1;
 4         while(left < right){
 5             char temp = s[left];
 6             s[left] = s[right];
 7             s[right] = temp;
 8             left++;
 9             right--;
10         }
11     }

 

Guess you like

Origin www.cnblogs.com/XRH2019/p/11829010.html