当前位置:网站首页>Leetcode- reverse string - simple

Leetcode- reverse string - simple

2022-06-13 05:47:00 AnWenRen

title :344 Reverse string - Simple

subject

Write a function , Its function is to invert the input string . Input string as character array s 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) To solve this problem .

Example 1

 Input :s = ["h","e","l","l","o"]
 Output :["o","l","l","e","h"]

Example 2

 Input :s = ["H","a","n","n","a","h"]
 Output :["h","a","n","n","a","H"]

Tips

  • 1 <= s.length <= 105
  • s[i] All are ASCII Printable characters in code table

Code Java

public void reverseString(char[] s) {
    
    int start = 0;
    int end = s.length - 1;
    while (start < end) {
    
        char temp = s[start];
        s[start] = s[end];
        s[end] = temp;
        start ++;
        end --;
    }
}
原网站

版权声明
本文为[AnWenRen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280507176567.html