leetcode344. The easiest force in the history of reversing a string buckle title

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.

 

Example 1:

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

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

Ideas: . . . It's that flip on the line.

class Solution {
    public void reverseString(char[] s) {
        int left = 0, right = s.length - 1;
        while (left < right) {
            char tmp = s[left];
            s[left++] = s[right];
            s[right--] = tmp;
        }
    }
}

 

Published 449 original articles · won praise 6893 · Views 1.04 million +

Guess you like

Origin blog.csdn.net/hebtu666/article/details/104071633