Reverse String

Write a function that takes a string as input and returns the string reversed.

Example: Given s = "hello", return "olleh".

Solution

public class Solution {
  public String reverseString(String s) {
        if (s == null || s.length() == 0 || s.length() == 1) {
            return s;
        }

        StringBuffer sb = new StringBuffer();
        for(int i = s.length() - 1; i >= 0; i --) {
            sb.append(s.charAt(i));
        }

        return sb.toString();
    }
}

results matching ""

    No results matching ""