美文网首页
Java单词倒序不使用类库

Java单词倒序不使用类库

作者: 小小恴乄牜 | 来源:发表于2018-08-10 10:24 被阅读8次

编写一个类似 一串英文 比如说 How old are you !? I don't understand 函数实现倒序:understand don't I !? you are old How。不准使用现有的类库 如 String StringBuffer ArrayList List Map 等

public char[] funaction (char[] input){

    //首先进行构造一个二维数组

    int twoCharLength = 1; //为了补全空格后+1
    for(int i = input.length - 1;i>= 0 ;i --){
        if(input[i] == ' '){
            twoCharLength++;
        }
    }

    char[][] tempTwoChar = new char[twoCharLength][];
    int index = 0;
    int tepmIndex = 0;
    for(int i = input.length - 1;i>= 0 ;i --){

        if(input[i] == ' '){
            tempTwoChar[tepmIndex++] = new char[index];
            index = 0;

        }else{
            index ++;
        }
    }

    tempTwoChar[tepmIndex] = new char[index];//将最后一个补全

    index = 0;
    int endIndex = 0;
    for(int i = input.length - 1;i>= 0 ;i --){
        if(input[i] != ' '){
            tempTwoChar[index][endIndex++] = input[i];
        }else{
            endIndex = 0;
            index++;
        }
    }
    index = 0;
    for(char[] counmChar:tempTwoChar){

        for(int i = counmChar.length - 1; i >= 0 ; i--){
            input[index++] = counmChar[i];
        }
        if(index < input.length){
            input[index++] = ' ';
        }


    }

    return input;
}

如有更好的思路或者更优的算法 欢迎留言探讨!风雨无阻、我在这里等你!

相关文章

网友评论

      本文标题:Java单词倒序不使用类库

      本文链接:https://www.haomeiwen.com/subject/kqckbftx.html