【问题标题】:Increment with recursion递归递增
【发布时间】:2023-04-05 12:37:01
【问题描述】:

我想通过递归来增加这个增量: 000 001 002 100 101 102 200 201 202 300 301 302

我知道从哪里开始和结束 String a = 000; String b = 302; 分别。 我正在尝试使用此代码:

private static void extendToMax(String l, String h)
{
    StringBuffer sb;
    for(int i=2; i>=0; i--)
    {
        sb = new StringBuffer(l);   
        if(charToDigit(l.charAt(i)) < charToDigit(h.charAt(i))) {           
            sb.replace(i, i+1, inc(sb.charAt(i)));  
            extendToMax(sb.toString(),h);
        }   
        if(checkElementary(sb.toString()))
        {
            if(array.indexOf(sb.toString())<0)
                array.add(sb.toString());
        }   
    }
}

但输出是001 002 102 202 302 201 202。数字202 不应重复。

【问题讨论】:

  • 现在增量是这样的:001 002 102 202 302 201 202
  • 你为什么使用递归?对于这个问题,迭代解决方案更有意义。
  • @JoeC 怎么样?它看起来有点像二进制,但我只想以最好的方式实现结果
  • 我不确定我是否理解序列...如果您的“范围”是 000 到 302,为什么要打印 303?
  • @Stefan 错了谢谢,它应该停在 302

标签: java recursion increment


【解决方案1】:

这是解决这个问题的最简单方法,使用递归。

第一个loop 方法只是为了让事情开始(它只是填充一个空的prefix)。第二个loop 方法对startstop 字符串的每个位置进行递归。当它通过最后一个位置时,它会打印结果(然后包含在prefix 中)。

private void loop(String start, String stop) {
    loop("", start, stop);
}

// Assume start.length==stop.length, and that start<stop at each position
private void loop(String prefix, String start, String stop) {
    if (start.length() > 0) {
        for (int c = start.charAt(0); c <= stop.charAt(0); c++) {
            loop(prefix + (char) c, start.substring(1), stop.substring(1));
        }
    } else {
        System.out.println(prefix);
    }
}

这适用于任何长度的字符串,例如从0010312115

【讨论】:

  • 可爱的解决方案!
  • 谢谢!当然,我在这里有点偏见,但我认为这应该是公认的答案:) 我不认为 extendToMax 解决方案真的有效。
  • Imo,它们已经排序了。你得到什么结果,你想得到什么结果?
【解决方案2】:

这是我的解决方案

static String l="000";
static String h="232";
static ArrayList<String> combinations = new ArrayList<String>();
static int stringLength= l.length();
for(int i=0; i<rulelength; i++)
{
    combinations.add((charToDigit(h.charAt(i)) - charToDigit(l.charAt(i))+1)+"");
}
int number = 1;
for(int i=0; i<combinations.size(); i++)
{
    number*=Integer.parseInt(combinations.get(i));
}
int change = Integer.parseInt(combinations.get(combinations.size()-1));
expand(l, h, change, number);

public static void expand(String l, String h, int change, int comb)
{ 
    StringBuffer sb = new StringBuffer(l);
    int pos = stringLength-1;
    int tmpPos = pos;
    for(int i=1; i<=comb; i++)
    {
        System.out.println(sb.toString());
        sb.replace(pos, pos+1, inc(sb.charAt(pos)));
        if((i % change)==0) {   
            for(int j=stringLength-1; j>0; j--)
            {
                if(charToDigit(sb.charAt(j)) >= (Integer.parseInt(combinations.get(j))-1)) 
                    tmpPos = j-1;                   
                else
                    break;
            }
            sb.replace(tmpPos, tmpPos+1, inc(sb.charAt(tmpPos)));
            for(int j=stringLength-1; j>tmpPos; j--)
            {
                sb.replace(j, j+1, l.charAt(j)+"");
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2018-10-11
    • 2023-03-25
    • 2017-12-07
    • 2013-04-12
    • 2012-11-26
    • 1970-01-01
    相关资源
    最近更新 更多