LeetCode - ReverseString  

by ne on 2022-08-26 under Algo/DS/Problems tagged with leetcode

The problem belongs to LeetCode, here is the link to the problem

Following code segment contains the description in the class comments, followed by fully working solution

 

 




/**
 * Write a function that reverses a string. The input string is given as an array of characters s.
 *
 * You must do this by modifying the input array in-place with O(1) extra memory.
 *
 * Example 1:
 *
 * Input: s = ["h","e","l","l","o"]
 * Output: ["o","l","l","e","h"]
 */
public class ReverseString {
    public void reverseString(char[] s) {
		// method;
        for(int i=0; i < s.length/2; i++){
            char t=s[i];
            s[i]=s[s.length-i-1];
            s[s.length-i-1]=t;
        }
    }
}