Programming Beauty 3.2 Phone number corresponding to the English word

Source: Internet
Author: User

Java implementations:


Import Java.util.arraylist;import Java.util.list;public class Telphone {/** * save alphabetical sequence for each number */static string[] c = new Stri Ng[] {"",//0 "",//1 "ABC",//2 "def",//3 "GHI",//4 "JKL",//5 "MNO",//6 "PQRS",//7 "TUV",//8 "WXYZ"//9};/** * Save The length of the letter sequence corresponding to each number */static int[] Total = {0, 0, 3, 3, 3, 3, 3, 4, 3, 4};p ublic static void Main (string[] args) {List<str ing> ret = find ("012"); for (String S:ret) {System.out.println (s);}} public static list<string> Find (String s) {arraylist<string> ret = new arraylist<string> (); if (s = = null Return Ret;ret.add (""); for (char Ch:s.tochararray ()) {int len = total[ch-' 0 '];//get the corresponding letter sequence length int size = Ret.size ();// RET current sizefor (int i = 0; i < len; i++) {for (int j = 0; J < size; J + +) {Ret.add (Ret.get (j) + C[ch-' 0 '].charat (i)) ;}} if (Size < Ret.size ())//To avoid 0, 1 o'clock, the next statement causes Ret.size==0ret = new Arraylist<string> (ret.sublist (Size, ret.size () ), or//keep the newly added element, that is, delete the 0-size-1 element}return ret;}}
Get the corresponding string, then query, judge is not a word, practical map dictionary, can O (1) time to achieve judgment;

Programming Beauty 3.2 Phone number corresponding to the English word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.