目录
前言
反转字符串,难度为入门级,本文提供了以下三种语言的核心代码。
一、C++
class Solution
{
public:
string solve(string str)
{
int len = str.size();
reverse(str.begin(), str.end());
return str;
// for(int i = 0; i < len/2; i++)
// {
// char temp = str[i];
// str[i] = str[len-i-1];
// str[len-i-1] = temp;
// }
// return str;
}
};
二、Java
import java.util.*;
public class Solution
{
public String solve (String str)
{
return new StringBuffer(str).reverse().toString();
}
}
// import java.util.*;
// import java.io.BufferedReader;
// import java.io.InputStreamReader;
// import java.io.IOException;
// public class Main {
// public static void main(String args[]) throws IOException{
// BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// String str;
// while((str=in.readLine())!=null){
// StringBuffer sb = new StringBuffer(str);
// sb.reverse();
// System.out.println(sb.toString());
// }
// }
// }
三、Javascript
function solve( str )
{
return str.split('').reverse().join("");
// split("")可以将字符串按某个字符或者其他分割。返回数组
// 如"hello".split("e") //["h","llo"]
// join是相反,把数组拼接为字符串。
// 如 ["a","b","c"].join(",") //"a,b,c"
}
module.exports =
{
solve :
solve
};