简体   繁体   English

Java递归电话号码字母

[英]Java recursion phone number letters

How do you write a java program using a recursive method that takes in an int like "234" and converts this into the corresponding letters on a phone pad (2 = ABC, 3 = DEF, etc), and prints out the permutations of this?您如何使用递归方法编写 java 程序,该方法接受一个像“234”这样的整数,并将其转换为手机键盘上的相应字母(2 = ABC,3 = DEF 等),并打印出这个的排列? eg:例如:

input = 234输入 = 234

output = ADG ADH ADI AEG AEH AEI AFG AFH AFI BDG BDH BDI BEG BEH BEI BFG BFH BFI CDG CDH CDI CEG CEH CEI CFG CFH CFI输出 = ADG ADH ADI AEG AEH AEI AFG AFH AFI BDG BDH BDI BEG BEH BEI BFG BFH BFI CDG CDH CDI CEG CEH CEI CFG CFH CFI


input = 89输入 = 89

output = TW TX TY TZ UW UX UY UZ VW VX VY VZ输出 = TW TX TY TZ UW UX UY UZ VW VX VY VZ

Generating permutations alone would constitute a good homework assignment, let alone the forced recursion and phone number distraction.单独生成排列将构成一个很好的家庭作业,更不用说强制递归和电话号码分心了。

Small numbers of permutations can be generated efficiently through a method analogous to sorting networks .通过类似于排序网络的方法,可以有效地生成少量排列。 A sequence of n elements has n! n 个元素的序列有n! permutations (assuming a full draw , with each permutation consisting of n elements as well).排列(假设一个完整的 draw ,每个排列也由n 个元素组成)。 Observe that for a two-element sequence, there are two permutations:观察到,对于二元素序列,有两种排列:

  • the original sequence, and原始序列,和
  • the two elements swapped.两个元素交换。

For a three-element sequence, there are six permutations:对于三元素序列,有六种排列:

  • the permutations of the bottom two elements, then底部两个元素的排列,然后
  • rotate the sequence one cell right (or left), and将序列向右(或向左)旋转一个单元格,以及
  • the permutations of the bottom two elements, then底部两个元素的排列,然后
  • rotate the sequence one cell left (or right, opposite of above), and将序列向左(或向右,与上述相反)旋转一个单元格,以及
  • the permutations of the bottom two elements.底部两个元素的排列。

That is, it's the two-element version performed three times, with two intervening transformations.也就是说,它是执行 3 次的双元素版本,中间有两次转换。 Now, a four-element sequence has twenty-four permutations:现在,一个四元素序列有 24 个排列:

  • the permutations of the bottom three elements, then底部三个元素的排列,然后
  • save position 1, assign 0 to 1, 3 to 0, and the saved value to 3, and保存位置1,将0赋给1,将3赋给0,将保存的值赋给3,然后
  • the permutations of the bottom three elements, then底部三个元素的排列,然后
  • swap positions 0 and 2, swap positions 1 and 3 (or rotate right by 2), and交换位置 0 和 2,交换位置 1 和 3(或向右旋转 2),以及
  • the permutations of the bottom three elements, then底部三个元素的排列,然后
  • save position 3, assign 2 to 3, 0 to 2, and the saved value to 0, and保存位置3,将2赋值给3,0赋值给2,保存的值赋值为0,然后
  • the permutations of the bottom three elements.底部三个元素的排列。

Are you starting to see a pattern?你开始看到一种模式了吗? Notice the recursive nature of the solution?注意到解决方案的递归性质了吗?

Above four elements, the patterns are harder to spot.以上四个元素,模式更难发现。 You can iterate down the sequence, swapping the chosen element with the last element, reversing the sequence, expanding the bottom 24 permutations each time.您可以向下迭代序列,将所选元素与最后一个元素交换,反转序列,每次扩展底部 24 个排列。

Try writing it out on paper, recording the steps you take to shuffle the elements, and you'll find you've written the program you need.试着把它写在纸上,记录你对元素进行洗牌的步骤,你会发现你已经编写了你需要的程序。


Note too that most of the solutions posted here use type String and keep chopping it up and reassembling it.另请注意,此处发布的大多数解决方案都使用String类型并不断将其切碎并重新组装。 String is a poor choice, given that it's immutable, when generating permutations is best done by swapping and rotating elements in a vector (really, a ring ). String是一个糟糕的选择,因为它是不可变的,当生成排列时,最好通过交换和旋转向量(实际上是ring )中的元素来完成。

It's the rare case that you actually have to write the letters out to the destination stream, so bias your solution against that operation.您实际上必须将信件写出到目标流是很少见的情况,因此请使您的解决方案偏向于该操作。 Use an array of characters and write the characters to the stream one-by-one when it's time to emit a particular permutation.使用字符数组并在需要发出特定排列时将字符一个一个地写入流。 Forming a string just to dump the entry to a stream involves unnecessary allocation and copying.形成一个字符串只是为了将条目转储到流中涉及不必要的分配和复制。

  1. Take input, convert to string获取输入,转换为字符串

  2. Call function generate(String prefix, String suffix) with empty prefix and converted input as suffix调用函数 generate(String prefix, String suffix) 前缀为空,转换后的输入为后缀

  3. Inside generate(), remove the first digit from suffix, map it to an array of corresponding letters and recursively call generate() for each letter from the array, appending it to the prefix.在 generate() 中,从后缀中删除第一个数字,将其映射到相应字母的数组,并为数组中的每个字母递归调用 generate(),将其附加到前缀。

import java.util.ArrayList;

class PhoneNumbers
{
    public static void main(String[] args)
    {
        for (String result: convert(args[0]))
            System.out.println(result);
    }

    public static ArrayList<String> convert(String phoneNumber)
    {           
        int digit = Integer.parseInt(phoneNumber.substring(0, 1));
        String letters = new String[] {
            "0",
            "1",
            "ABC",
            "DEF",
            "GHI",
            "JKL",
            // etc...
        }[digit];

        ArrayList<String> result = new ArrayList<String>();

        for (int i = 0; i < letters.length(); ++i) {
            char letter = letters.charAt(i);
            if (phoneNumber.length() > 1) {
                for (String rest: convert(phoneNumber.substring(1)))
                    result.add(letter + rest);
            } else {
                result.add("" + letter);
            }
        }

        return result;
    }
}

java PhoneNumbers 234 java电话号码234

ADG
ADH
ADI
AEG
AEH
AEI
AFG
AFH
AFI
...

Here's a version without either array or arraylist.这是一个没有数组或数组列表的版本。 Results are printed to stdout as you requested.结果按照您的要求打印到标准输出。

String[] allLetters = new String[] {
                "0",
                "1",
                "ABC",
                "DEF",
                "GHI",
                "JKL",
                // etc...
        };

public static void convert(String phoneNumber)
{
  convertSubstring(phoneNumber,"");
}

private static void convertSubstring(String phoneNumber, String convertedLetters)
{                   
  int digit = Integer.parseInt(phoneNumber.substring(0, 1));
  String letters=allLetters[digit];
  String remainingString=phoneNumber.substring(1);

  for (int i = 0; i < letters.length(); ++i) 
  {
     char letter = letters.charAt(i);
     String result=convertedLetters+letter;
     if (remainingString.length()==0)
        System.out.println(result);
     else
        convertSubstring(remainingString, result);
  }
}
public class printKeypad {
public static char[] keynotes(int n)
{
    String s[]= {"","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    String s1=s[n-1];
    char c[]=new char[s1.length()];
    for(int i=0;i<c.length;i++)
    {
        c[i]=s1.charAt(i);
    }
    return c;
}
public static void printKeypad(int n)
{
    printKeypad(n,"");
}
public static void printKeypad(int n,String output)
{
    if(n==0)
    {
        System.out.println(output);
        return;
    }
    char c[]=keynotes(n%10);
    for(int i=0;i<c.length;i++)
    {
        printKeypad(n/10,output+c[i]);
    }
}
public static void main(String[] args) {
    int n=23;
    printKeypad(n);
}

} //both the printing as well as the returning of keypad are solved using different //approaches } //打印和键盘返回都使用不同的方法来解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM