首页 > 试题广场 >

反转字符串

[编程题]反转字符串
  • 热度指数:150183 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)

数据范围: 
要求:空间复杂度 ,时间复杂度
示例1

输入

"abcd"

输出

"dcba"
示例2

输入

""

输出

""
public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        //把String变成char[]
        char[] charr=str.toCharArray();
        int l=0;
        int r=charr.length-1;
        char tmp;
        while(l<r){
            tmp=charr[l];
            charr[l]=charr[r];
            charr[r]=tmp;
            l++;
            r--;
        }
        String ans=String.valueOf(charr);
        return ans;
    }
}

发表于 2023-05-06 21:47:33 回复(0)
最简单的写法 😁 
public String solve (String str) {
    // write code here
    String specialCode = "\u202e";
    String result = specialCode + str;
    return result;
}

发表于 2023-04-25 17:33:01 回复(0)
import java.util.*;


public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        return new StringBuffer(str).reverse().toString();
    }
}

发表于 2023-03-07 15:03:58 回复(0)
public String solve (String str) {
        char arr[] = str.toCharArray();
        String a = "";
        for (int i = arr.length - 1; i >= 0; i--) {
            a+=arr[i];
        }
        return a;
    }
发表于 2022-09-27 14:22:20 回复(0)
return new StringBuffer(str).reverse().toString();
java一句话解决
发表于 2022-08-24 15:39:34 回复(0)
import java.util.*;

public class Solution {

    public String solve (String str) {
        StringBuilder sb = new StringBuilder(str);
        return sb.reverse().toString();
    }
}

发表于 2022-08-08 16:16:21 回复(0)
import java.util.*;
public class Solution {
    public String solve (String str) {
        StringBuilder sb = new StringBuilder(str);
        sb.reverse();
        return new String(sb);
    }
}


发表于 2022-08-07 15:19:38 回复(1)
    public String solve (String str) {
        // write code here
        char[] cs = new char[str.length()];
        for(int i=0;i<cs.length;i++){
            cs[i]=str.charAt(cs.length-1-i);
        }
        return String.valueOf(cs);
    }

发表于 2022-06-06 00:14:44 回复(0)
入门级的题居然没有一次通过,我实在太low了
import java.util.*;
public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        if(str.length() == 0 || str.length() == 1){
            return str;
        }
        char[] chr = str.toCharArray();
        reverse(chr);
        return String.valueOf(chr);
    }
    public void reverse(char[] chr){
        int l = 0;
        int r = chr.length -1;
        while(l < r){
            char temp = chr[l];
            chr[l] = chr[r];
            chr[r] = temp;
            l++;
            r--;
        }
    }
}

发表于 2022-05-03 16:07:50 回复(0)
import java.util.*;

public class Solution {
    public String solve (String str) {
        // write code here
        StringBuilder data = new StringBuilder(str);
        str = data.reverse().toString();
            return str;
    }
}

发表于 2022-04-16 10:41:19 回复(0)
public static void main(String[] args) 
就是说,为什么没有main函数
发表于 2022-04-09 15:38:53 回复(0)
public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        char[] c = str.toCharArray();
       String res = new String();
        for(int i = str.length()-1;i>=0;i--){
            res += c[i];
        }
        return res;
    }
}
发表于 2022-02-22 18:48:04 回复(0)
import java.util.*;


public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        return new StringBuilder(str).reverse().toString();
    }
}

发表于 2022-01-06 10:15:34 回复(0)
import java.util.*;


public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        StringBuilder sb = new StringBuilder(str);
        sb.reverse();
        return sb.toString();
    }
}

发表于 2021-12-13 16:21:18 回复(0)
import java.util.*;


public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
        StringBuilder strResult = new StringBuilder();
        char[] chars = str.toCharArray();
        for(int i = chars.length-1;i>=0;i--){
            strResult.append(chars[i]);
        }
        return strResult.toString();
    }
}

发表于 2021-12-10 15:57:35 回复(0)
使用java StringBuffer的reverse方法也很简单
        StringBuffer temp= new StringBuffer();
        temp.append(str);
        return temp.reverse().toString();
发表于 2021-12-01 23:20:12 回复(0)

package demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* All rights Reserved, Designed By beijingdiaoyu
* @Title:Demo02.java
* @Package:demo
* @Description: 反转字符串
* @author: 钓鱼
* @date: 2021年11月21日
* @version:v1.0
* @Copyright: 2021  Inc.beijingdiaoyu All rights reserved.
*/
public class Demo02 {


    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        System.out.println("请输入要反转的字符串:");
        Scanner in = new Scanner(System.in);
        String str = in.next();    
        char[] list=str.toCharArray();
        System.out.println("反转后的字符串为:");
        for(int i = 0;i<=str.length()-1;i++) {
            list[i] = str.charAt(str.length()-1-i);
            System.out.print(list[i]);
        }
    }

}

发表于 2021-11-21 20:24:10 回复(0)
import java.util.*;


public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
        // write code here
      char t [] = str.toCharArray();
       int h=str.length(); 
        for(int i=0;i<(int)(h/2);i++){
            
            char temp=t[i];
            t[i]=t[h-i-1];
            t[h-i-1]=temp;
            
            
        }
       str = String.valueOf(t);
        return str;
       
    }
}

发表于 2021-11-19 15:29:54 回复(0)
import java.util.*;
public class Solution {
    public String solve (String str) {
        StringBuffer sb = new StringBuffer();
        int len = str.length() - 1;
        while(len >= 0 )
            sb.append(str.charAt(len--));
        return sb.toString();
    }
}
不知道这样算不算优化了空间
发表于 2021-11-15 14:39:59 回复(0)
import java.util.*;


public class Solution {
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
      char[] chars = str.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i <chars.length ; i++) {
            sb.append(chars[chars.length-1-i]);
        }
        return sb.toString();
    }
}

发表于 2021-11-04 14:31:01 回复(0)

问题信息

上传者:牛客332641号
难度:
55条回答 15636浏览

热门推荐

通过挑战的用户

查看代码