当前位置:网站首页>Leetcode question brushing: String 01 (inverted string)

Leetcode question brushing: String 01 (inverted string)

2022-06-26 20:49:00 Taotao can't learn English

344. Reverse string

Force button topic link

Write a function , Its function is to invert the input string . Input string as character array char[] Given in the form of .

Do not allocate extra space to another array , You have to modify the input array in place 、 Use O ( 1 ) O(1) O(1) To solve this problem .

You can assume that all characters in an array are ASCII Printable characters in code table .

Example 1:
Input :[“h”,“e”,“l”,“l”,“o”]
Output :[“o”,“l”,“l”,“e”,“h”]

Example 2:
Input :[“H”,“a”,“n”,“n”,“a”,“h”]
Output :[“h”,“a”,“n”,“n”,“a”,“H”]

This question is simply not too simple for me who just finished the sum of four numbers of the double pointer method

    public void reverseString(char[] s) {
    
        // Double finger needling 
        int left = 0;
        int right = s.length - 1;
        while (left < right) {
    
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
    }

 Insert picture description here

 you 're right , Again AC. Powerful .
原网站

版权声明
本文为[Taotao can't learn English]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262029503575.html