LeetCode算法题——返回字符串表示的电话按键所有字母组合

题目

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

 

代码实现

public class day {
	
	public static ArrayList twoFor(String x,ArrayList first_List) {
		
		int count = 0;
		ArrayList end_List = new ArrayList<>();
		
		for(int j=0;j onefor(String x,ArrayList Beginning_list){
		
		int count = 0;
		for(int j=0;j first_List,String input) {
				
		String x2 = "abc";
		String x3 = "def";
		String x4 = "ghi";
		String x5 = "jkl";
		String x6 = "mno";
		String x7 = "pqrs";
		String x8 = "tuv";
		String x9 = "wxyz";
		
		ArrayList end_List = new ArrayList<>();
		
		if(input.charAt(0)=='2') {
			if(first_List.size()==0) {
				end_List=onefor(x2,first_List);
			}else {
				end_List = twoFor(x2, first_List);
			}
		}else if(input.charAt(0)=='3') {
			if(first_List.size()==0) {
				end_List=onefor(x3,first_List);
			}else {
				end_List = twoFor(x3, first_List);
			}
		}else if(input.charAt(0)=='4') {
			if(first_List.size()==0) {
				end_List=onefor(x4,first_List);
			}else {
				end_List = twoFor(x4, first_List);
			}
		}else if(input.charAt(0)=='5') {
			if(first_List.size()==0) {
				end_List=onefor(x5,first_List);
			}else {
				end_List = twoFor(x5, first_List);
			}
		}else if(input.charAt(0)=='6') {
			if(first_List.size()==0) {
				end_List=onefor(x6,first_List);
			}else {
				end_List = twoFor(x6, first_List);
			}
		}else if(input.charAt(0)=='7') {
			if(first_List.size()==0) {
				end_List=onefor(x7,first_List);
			}else {
				end_List = twoFor(x7, first_List);
			}
		}else if(input.charAt(0)=='8') {
			if(first_List.size()==0) {
				end_List=onefor(x8,first_List);
			}else {
				end_List = twoFor(x8, first_List);
			}
		}else{
			if(first_List.size()==0) {
				end_List=onefor(x9,first_List);
			}else {
				end_List = twoFor(x9, first_List);
			}
		}
		String end_string = input.substring(1);
		if(end_string.equals("")) {
			
			System.out.println(end_List.size());
			for(String end:end_List) {
				System.out.print(end+" ");
			}
		}else {
			phone_number(end_List,end_string);
		}
	}
	
	public static void main(String[] args) {
	
		int input = 2479;
		String input_string = String.valueOf(input);
		ArrayList loop = new ArrayList<>();
		
		phone_number(loop,input_string);
 	}
}

 

你可能感兴趣的:(算法)