猿问

如何修复CodingBat中的递归代码错误?

所以,我正在 CodingBat 上解决这个问题

给定一个字符串,递归(无循环)计算一个新字符串,其中所有出现的“pi”都已替换为“3.14”。

更改Pi(“xpix”)→“x3.14x”

更改Pi(“pipi”)→“3.143.14”

ChangePi("pip") → "3.14p"

这是我的代码:

public String changePi(String str) {

    if (str.length() < 2)

        return str;

    char c1 = str.charAt(0);

    char c2 = str.charAt(1);

    if (c1 == 'p' && c2 == 'i')

        return "3.14" + changePi(str.substring(2));

    return c1 + c2 + changePi(str.substring(2));

}

此代码不适用于许多测试用例,如下图所示

我无法理解我的递归代码出了什么问题以及为什么它显示这样的输出。谁能帮助我理解我做错了什么?



守候你守候我
浏览 171回答 3
3回答

SMILET

您的解决方案需要一个小调整 - 当您找到 ap和i在一起时,您正确地向前跳过 2 个字符以查看字符串的其余部分 - 您正在使用return "3.14" + changePi(str.substring(2));.但是,当您没有找到和p在一起i时,您需要更改逻辑以仅向前跳过一个字符而不是两个字符。所以代替这个:return c1 + c2+ changePi(str.substring(2));做这个:return c1 + changePi(str.substring(1));进行更改后,我得到以下输出(使用您的每个输入),该输出与您的预期输出相匹配:x3.14x3.143.143.14p3.14hippx3.14xxxyzzy

沧海一幻觉

这里有几个解决该问题的方法。第二种和你的方法类似。在第一个中,我最初包含了目标替换文本,因此我需要确定其长度。因此,"pi".length()我决定将其保留下来。&nbsp; &nbsp; &nbsp; &nbsp;public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] tests = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "piabcpiefgpi", "xxxxxxpix", "xpix", "pip", "3.14", "3.14p",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "hip", "p", "x", "", "pixx", "xyzzy"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String t : tests) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(t + " --> " + replaceV2(t));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;public static String replaceV1(String a) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i = a.indexOf("pi");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return replaceV1(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.substring(0, i) + "3.14" + a.substring(i + "pi".length()));&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;public static String replaceV2(String a) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a.length() < 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return a;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a.startsWith("pi")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a = "3.14" + replaceV2(a.substring(2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return a.substring(0, 1) + replaceV2(a.substring(1));&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }他们都打印以下内容。piabcpiefgpi --> 3.14abc3.14efg3.14xxxxxxpix --> xxxxxx3.14xxpix --> x3.14xpip --> 3.14p3.14 --> 3.143.14p --> 3.14phip --> hipp --> px --> x&nbsp;-->&nbsp;pixx --> 3.14xxxyzzy --> xyzzy

RISEBY

'''&nbsp; change pi to 3.14 python code'''def changePi(s):&nbsp; &nbsp; if len(s) == 2 and s == 'pi' :&nbsp; return '3.14'&nbsp; &nbsp; if len(s) <= 2 :&nbsp; return s&nbsp;&nbsp; &nbsp; chars , sub = s[0:2] , s[1:]&nbsp; &nbsp; if chars == 'pi':&nbsp; &nbsp; &nbsp; &nbsp;chars = '3.14'&nbsp; &nbsp; &nbsp; &nbsp;sub = sub[1:]&nbsp; &nbsp; else:&nbsp; chars =&nbsp; chars[0]&nbsp; &nbsp; return chars + changePi(sub)&nbsp;print changePi("xpix")&nbsp;print changePi("pipi")&nbsp;print changePi("pip")
随时随地看视频慕课网APP

相关分类

Java
我要回答