@Test public void solution() { assertNull(ReverseStringSolution.reverseString(null)); assertEquals("", ReverseStringSolution.reverseString("")); assertEquals("a", ReverseStringSolution.reverseString("a")); assertEquals("olleh", ReverseStringSolution.reverseString("hello")); assertEquals("!dlrow", ReverseStringSolution.reverseString("world!")); long b = System.currentTimeMillis(); int count = SUPPRESS_TEST_LIMITATION; while (count > 0) { count--; ReverseStringSolution.reverseString("yuzhouwan.com"); } long e = System.currentTimeMillis(); System.out.println(e - b); }
static String reverseString(String s) { int len; if (s == null || (len = s.length()) <= 1) { return s; } char[] chars = new char[len]; char[] result = new char[len]; s.getChars(0, len, chars, 0); for (int i = 0; i < len; i++) { result[len - i - 1] = chars[i]; } return new String(result); }
ReverseStringSolution { static String reverseString(String s) { int len; if (s == null || (len = s.length()) <= 1) { return s; } char[] chars = new char[len]; char[] result = new char[len]; s.getChars(0, len, chars, 0); for (int i = 0; i < len; i++) { result[len - i - 1] = chars[i]; } return new String(result); } }
ReverseStringSolution { static String reverseString(String s) { int len; if (s == null || (len = s.length()) <= 1) { return s; } char[] chars = new char[len]; char[] result = new char[len]; s.getChars(0, len, chars, 0); for (int i = 0; i < len; i++) { result[len - i - 1] = chars[i]; } return new String(result); }  }
ReverseStringSolution { static String reverseString(String s) { int len; if (s == null || (len = s.length()) <= 1) { return s; } char[] chars = new char[len]; char[] result = new char[len]; s.getChars(0, len, chars, 0); for (int i = 0; i < len; i++) { result[len - i - 1] = chars[i]; } return new String(result); }   }
ReverseStringSolution { static String reverseString(String s) { int len; if (s == null || (len = s.length()) <= 1) { return s; } char[] chars = new char[len]; char[] result = new char[len]; s.getChars(0, len, chars, 0); for (int i = 0; i < len; i++) { result[len - i - 1] = chars[i]; } return new String(result); }    }