• java实现字符串反转


    代码:

    import java.util.Stack;
    
    public class StringReverse {
        public static void main(String[] args) {
            String s = "hello";
            String ss = reverse7(s);
            System.out.println(ss);
        }
    
        // 使用内置类(StringBuilder或StringBuffer)
        public static String reverse1(String s) {
            // StringBuilder strBuilder = new StringBuilder(s);
            // String ret = strBuilder.reverse().toString();
            StringBuffer strBuf = new StringBuffer(s);
            String ret = strBuf.reverse().toString();
            return ret;
        }
    
        // 有左到右 拼接字符串
        public static String reverse2(String s) {
            String ret = "";
            for (int i = 0; i < s.length(); ++i) {
                ret = s.charAt(i) + ret;
            }
            return ret;
        }
    
        // 从右到左 拼接字符串
        public static String reverse3(String s) {
            String ret = "";
            for (int i = s.length() - 1; i >= 0; --i) {
                ret += s.charAt(i);
            }
            return ret;
        }
    
        public static String reverse4(String s) {
            String ret = "";
            char[] charArr = s.toCharArray();
            int len = charArr.length;
            for (int i = 0; i < len / 2; ++i) {
                char tmp = charArr[i];
                charArr[i] = charArr[len - 1 - i];
                charArr[len - 1 - i] = tmp;
            }
            ret = new String(charArr);
            return ret;
        }
    
        // 使用异或
        public static String reverse5(String s) {
            String ret = "";
            char[] charArr = s.toCharArray();
            int begin = 0, end = charArr.length - 1;
            while (begin < end) {
                charArr[begin] = (char) (charArr[begin] ^ charArr[end]);
                charArr[end] = (char) (charArr[begin] ^ charArr[end]);
                charArr[begin] = (char) (charArr[begin] ^ charArr[end]);
    
                begin++;
                end--;
            }
            ret = new String(charArr);
            return ret;
        }
    
        // 使用栈
        public static String reverse6(String s) {
            String ret = "";
            Stack<Character> stack = new Stack<Character>();
            for (int i = 0; i < s.length(); ++i) {
                stack.push(s.charAt(i));
            }
            while (!stack.isEmpty()) {
                ret += stack.pop();
            }
    
            return ret;
        }
    
           // 使用递归
        public static String reverse7(String s) {
            if (s.length() == 1)
                return s;
            return reverse7(s.substring(1)) + s.charAt(0);
        }
    }
  • 相关阅读:
    find命令详解
    wget命令
    国内镜像源
    向linux服务器上传下载文件方式收集
    一些初学shell自己写的一些练习题脚本
    在Linux系统下mail命令的用法
    MAC 下安装 SVN
    天气预报api整理
    pdi vcard-2.1
    Android Studio 问题锦集【持续更新】
  • 原文地址:https://www.cnblogs.com/hupeng1234/p/6830164.html
Copyright © 2020-2023  润新知