程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Letter Combinations of a Phone Number

Letter Combinations of a Phone Number

編輯:C++入門知識

題目原型:

Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

\

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

基本思路:

遞歸求解

	public ArrayList letterCombinations(String digits)
	{
		ArrayList list = new ArrayList();
		char[] number = digits.toCharArray();//存放電話號碼
		int len = number.length;//電話號碼長度
		String[] phone = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};//字母與數字對應表
		int[] total = {0,0,3,3,3,3,3,4,3,4};//每個數字上有多少個字母
		int[] answer = new int[len];//指示每個數字所對應字母序列的字符位子,如第一個數字是2,如果answer[0] = 0,表示所對應的字符是a即phone[0].charAt[answer[0]]='a'
		combinatons(list,phone, number, answer, 0, len, total);
		return list;
	}
	
	void combinatons(ArrayList list,String[] phone,char[] number , int[] answer , int index , int len , int[] total)
	{
		if(index==len)
		{
			StringBuffer strbuf = new StringBuffer();
			for(int i = 0 ;i


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved