递归将字符移动到字符串的末尾 [英] recursion moving char to the end of the string

查看:64
本文介绍了递归将字符移动到字符串的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取一个字符串并通过获取字符来递归重新排列它,并且通过该字符我必须将该字符移动到字符串上的任何地方到最后比如世界你好!",'l' => "Heo word!lll"我在理解递归思维方式时遇到问题所以我从这个开始:

i need to get a string and rearrange it with recursion by getting char and by that char i have to move that char everywhere on the string to the end like "Hello world!" ,'l' => "Heo word!lll" i have problems understading the recursion way of thinking so i started with this:

public static String ChToLast (String str, char ch){
    if(str.indexOf(ch)== -1){
        return str;
    }else{
        if(str.indexOf(0) == ch){
            return str;
        }
    }

谢谢你的帮助:)

推荐答案

递归是在其内部重用方法的实践.在这种情况下,我将提供一个解决方案来解释会发生什么:

Recursion is the practise of reusing your method inside itself. In this case, I will provide a solution to explain what happens:

public static String chrToLast(String str, char ch) {
    //This if statement details the end condition
    if(str.length() < 1) {
        return "";
    }

    String newString = str.substring(1); //Create new string without first character

    if(str.indexOf(ch) == 0) { //This happens when your character is found
        return chrToLast(newString, ch) + ch;
    } else { //This happens with all other characters
        return str.charAt(0) + chrToLast(newString, ch);
    }
}

如果你执行:

chrToLast("Hello, World!", 'l')

这将产生所需的结果:Heo, Word!lll

过程

一般来说,此方法的工作原理是检查当前给定字符串中的第一个字符,然后决定要做什么.如果第一个字符与您要查找的字符相同 (l),则它将从字符串中删除该字符并在 该新字符串上使用 chrToLast.但是,它还使用 + ch 将找到的字符添加到结果的末尾.它会继续这样做,直到没有更多字符剩下,这就是 结束条件 的用途.

In general, this method works by checking which character is currently the first in the given string, and then deciding what to do. If the first character is the same as the one your looking for (l), it will then remove that character from the string and use chrToLast on that new string. But, it also adds the character it found to the end of the result by using + ch. It continues to do this until there are no more characters left, which is what the end condition is for.

结束条件

结束条件返回一个空字符串"",因为这就是算法的基本情况.您可以将递归算法视为通过多次调用自身来解决问题的方法.通过调用自己,递归算法向移动.在这种特殊情况下,它通过每次执行该方法时从字符串中减去一个字符来实现这一点.一旦没有剩余字符,它就会到达基本情况,即 "",其中字符串最终为空,并且不能再减去任何字符.(因此它在最终状态时不返回任何内容)

The end condition returns an empty string "" because that is what is called the base case of the algorithm. You can think of a recursive algorithm as something solving a problem by calling itself a number of times. By calling themselves, recursive algorithms move towards a base. In this particular case, it does that by subtracting one character off the string each time the method is executed. Once there are no characters left, it reaches the base case which is "", where the string is finally empty and no characters can be subtracted anymore. (Hence it returns nothing as it's final state)

我希望这能回答您的问题.理解这个概念很重要,因为它非常强大.如果有不清楚的地方,请尝试研究代码和评论.

I hope this answers your question. It's important to understand this concept, as it is very powerful. Try to study the code and comment if something's not clear.

在 IDE 中执行此代码并使用调试器遍历其执行过程也有帮助.然后你就可以亲眼看看程序的流程是什么,看看在起作用的变量的值.

Something that can also help is by executing this code in an IDE and using the debugger to walk through its execution. You can then see for yourself what the flow of the program is, and see the value of the variables in play.

这篇关于递归将字符移动到字符串的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆