反转字符串和句子的几种方式(Java)

反转字符串:

asd fgh => hgf dsa

反转句子:

asd fgh => fgh asd

 

要求:

尽量不使用额外空间(仅在str转换的char[]中操作)

 

代码:

package com.my.test.datastructure.str;

/**
 * 句子反转:
 * ABC DEF => DEF ABC
 * 
 * 字符串反转:
 * ABC DEF => FED CBA
 * 
 * https://blog.csdn.net/EuniBoom/article/details/80136800
 */
public class ReverseStrOrSentence
{
    
    /**
     * 反转句子(丢弃了句子结尾的空格)
     * 
     * 利用StringBuilder + split实现 需要额外空间
     * 
     * ABC DEF => DEF ABC
     */
    public static String reverseSentenceBySB(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        final String space = " ";
        String[] arr = str.split(space);
        StringBuilder sb = new StringBuilder();
        int len = arr.length;
        for (int i=len-1; i>=0; i--) {
            // 不要忘记需要加上空格
            if (i != len - 1) {
                sb.append(space);
            }
            sb.append(arr[i]);
        }
        return sb. toString();
    }
    
    /**
     * ABC DEF => DEF ABC
     * 
     * 利用StringBuilder + toCharArray实现 需要额外空间
     * 
     */
    public static String reverseSentenceByCharArr(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        char[] arr = str.toCharArray();
        StringBuilder sb = new StringBuilder();
        int len = arr.length;
        int pos = 0;
        for (int i=0; i I am a student 
     */
    public static String reverseSentence(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        char[] arr = str.toCharArray();
        
        // 先全量反转整个字符串
        int len = arr.length;
        reverseCharArr(arr, 0, len - 1);
        
        // 在根据空格分割,分别反转单词
        int start = 0;
        for (int i = 0; i < len ; i++) {
            // 遇到空格反转单词
            if (arr[i] == ' ') {
                reverseCharArr(arr, start, i - 1);
                start = i + 1;
            // 句子最后反转单词
            } else if (i == len - 1) {
                reverseCharArr(arr, start, i);
            }
        }
        
        return new String(arr);
    }
    
    /**
     * 翻转字符数组的部分元素,start为起始索引,end为终止索引
     */
    private static void reverseCharArr(char[] arr, int start, int end) {
        // 只有一个单词或参数不合法,不进行反转
        if (end - start < 1) {
            return;
        }
        // 内部函数,调用处保证参数合法性,无需校验
        int i = start;
        int j = end;
        while (i FEB CBA
     * 
     * 利用StringBuilder实现 性能较低
     * 
     */
    public static String reverseStrBySB(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        return sb.reverse().toString();
    }

    /**
     * ABC DEF => FEB CBA
     * 
     * 利用StringBuilder + toCharArray实现 需要额外空间
     * 
     */
    public static String reverseStrByCharArr(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        char[] arr = str.toCharArray();
        int len = arr.length;
        
        StringBuilder sb = new StringBuilder();
        for (int i=len-1; i>=0; i--) {
            sb.append(arr[i]);
        }
        return sb.toString();
    }
    
    /**
     *  翻转字符串,不使用额外空间
     *  例如:asd fgh => hgf dsa
     */
    public static String reverseStr(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        char[] arr = str.toCharArray();
        // 翻转字符数组
        reverseCharArr(arr, 0, arr.length - 1);
        return new String(arr);
    }
    
    public static void main(String[] args)
    {
        String str = "ABC DEF GHI  JK ";
        System.out.println(reverseSentenceBySB(str));
        System.out.println(reverseSentenceByCharArr(str));
        System.out.println(reverseSentence(str));
        
        System.out.println(reverseStrBySB(str));
        System.out.println(reverseStrByCharArr(str));
        System.out.println(reverseStr(str));
    }

}

 

参考:

反转一个英文句子:https://blog.csdn.net/kakascx/article/details/52226382

反转字符串的7种方法:https://blog.csdn.net/EuniBoom/article/details/80136800

将英文句子倒转:https://blog.csdn.net/yk3372/article/details/53153311

你可能感兴趣的:(剑指Offer)