Skip to content

Latest commit

 

History

History

0344-reverse-string

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Problem Description

Write a function that reverses a string. The input string is given as an array of characters char[].

Detail instruction can be found here.

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"]

Solution

public void reverseString(char[] s) {
    int n = s.length;
    for (int i = 0; i < n / 2; i++) {
        exch(s, i, n - i - 1);
    }
}

private helper methods

private void exch(char[] s, int i, int j) {
    char c = s[i];
    s[i] = s[j];
    s[j] = c;
}

This is only for discussion and communication. Please don't use this for submission of assignments.