344. Reverse String

来源:互联网 发布:java项目开发实战案例 编辑:程序博客网 时间:2024/05/22 12:11
Write a function that takes a string as input and returns the string reversed.


Example:

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


Answer:


public class Solution {    public String reverseString(String s) {        StringBuilder temp = new StringBuilder();        for(int i=0;i<s.length();i++){            char c =s.charAt(i);            temp.insert(0,c);        }        return  temp.toString();    }}


思路:翻转字符串。通过StringBuilder的insert方法,可以将每个字符重新插入进新的字符串中,返回即可。

原创粉丝点击