Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
This problem is incredibly simple. Here are two solutions: one is from when I first started doing this problem, and the other is from a month later (doesn't it feel like a significant improvement?).
Similar questions:https://blog.jing.do/4772
public class Solution { public String reverseString(String s) { char[] sc = s.toCharArray(); char[] res = new char[sc.length]; int l=0; for(int i=sc.length-1; i>=0;i--){ res[l] = sc[i]; l++; } return new String(res); } } public class Solution { public String reverseString(String s) { char[] word = s.toCharArray(); int i=0; int l=word.length -1; while(i This websiteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:
Original author:Jake Tao,source:「LeetCode – 344. Reverse String」