1. 程式人生 > >【LeetCode-面試演算法經典-Java實現】【017-Letter Combinations of a Phone Number (電話號碼上的單詞組合)】

【LeetCode-面試演算法經典-Java實現】【017-Letter Combinations of a Phone Number (電話號碼上的單詞組合)】

原題

  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 class Solution {

    private String[] map = {
            "abc",
            "def",
            "ghi",
            "jkl",
            "mno"
, "pqrs", "tuv", "wxyz", }; private List<String> result; // 儲存最終結果 private char[] chars; // 儲存去掉0,1字元的結果 private char[] curResult; // 儲存中間結果 private int end = 0; // 字元陣列中的第一個未使用的位置 private int handle = 0; // 當前處理的是第幾個字元數字
public List<String> letterCombinations(String digits) { result = new LinkedList<>(); if (digits != null && digits.length() > 0) { chars = digits.toCharArray(); // 對字串進行處理,去掉0和1 // 找第一個0或者1的位置 while (end < digits.length() && chars[end] != '0' && chars[end] != '1') { end++; } handle = end + 1; while (handle < chars.length) { if (chars[handle] != '0' && chars[handle] != '1') { chars[end] = chars[handle]; end++; } handle++; } curResult = new char[end]; // while結束後,end為有效字元的長度 handle = 0; // 指向第一個有效字元的位置 letterCombinations(); } return result; } private void letterCombinations() { if (handle >= end) { result.add(new String(curResult)); } else { int num = chars[handle] - '2'; for (int i = 0; i < map[num].length(); i++) { curResult[handle] = map[num].charAt(i); handle++; letterCombinations(); handle--; } } } }

評測結果

  點選圖片,滑鼠不釋放,拖動一段位置,釋放後在新的視窗中檢視完整圖片。
這裡寫圖片描述

特別說明

相關推薦

LeetCode-面試演算法經典-Java實現017-Letter Combinations of a Phone Number 電話號碼單詞組合

原題   Given a digit string, return all possible letter combinations that the number could rep

LeetCode-面試演算法經典-Java實現067-Add Binary二進位制加法

原題   Given two binary strings, return their sum (also a binary string).   For example,   a

LeetCode-面試演算法經典-Java實現151-Reverse Words in a String反轉字串中的單詞

原題   Given an input string, reverse the string word by word.   For example,   Given s = "the sky is blue",   return "bl

LeetCode-面試演算法經典-Java實現136-Single Number只出現一次的數字

原題   Given an array of integers, every element appears twice except for one. Find that singl

LeetCode-面試演算法經典-Java實現153-Find Minimum in Rotated Sorted Array找旋轉陣列中的最小數字

原題   Suppose a sorted array is rotated at some pivot unknown to you beforehand.   (i.e., 0

LeetCode-面試演算法經典-Java實現021-Merge Two Sorted Lists合併兩個排好序的單鏈表

原題   Merge two sorted linked lists and return it as a new list. The new list should be made

LeetCode-面試演算法經典-Java實現019-Remove Nth Node From End of List移除單鏈表的倒數第N個節點

原題   Given a linked list, remove the nth node from the end of list and return its head.   F

LeetCode-面試演算法經典-Java實現003-Longest Substring Without Repeating Characters最長非重複子字串

原題   Given a string, find the length of the longest substring without repeating characters. For

LeetCode-面試演算法經典-Java實現064-Minimum Path Sum最小路徑和

原題   Given a m x n grid filled with non-negative numbers, find a path from top left to botto

LeetCode-面試演算法經典-Java實現198-House Robber搶劫犯

原題   You are a professional robber planning to rob houses along a street. Each house h

LeetCode-面試演算法經典-Java實現142-Linked List Cycle II單鏈表中有環II

原題   Given a linked list, return the node where the cycle begins. If there is no cycle, retu

LeetCode-面試演算法經典-Java實現134-Gas Station加油站問題

原題   There are N gas stations along a circular route, where the amount of gas at station i i

LeetCode-面試演算法經典-Java實現110-Balanced Binary Tree平衡二叉樹

原題   Given a binary tree, determine if it is height-balanced.   For this problem, a height-

LeetCode-面試演算法經典-Java實現154-Find Minimum in Rotated Sorted Array II找旋轉陣列中的最小數字II

原題   Follow up for “Find Minimum in Rotated Sorted Array”:   What if duplicates are allow

LeetCode-面試演算法經典-Java實現206-Reverse Linked List反轉一個單鏈表

原題   Reverse a singly linked list. 題目大意   反轉單鏈表。 解題思路   使用頭插法。 程式碼實現

LeetCode-面試演算法經典-Java實現006-ZigZag ConversionZ字型轉換

原題   The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows

LeetCode-面試演算法經典-Java實現073-Climbing Stairs爬樓梯

原題   You are climbing a stair case. It takes n steps to reach to the top.   Each time you c

LeetCode-面試演算法經典-Java實現165-Compare Version Numbers比較版本號

原題   Compare two version numbers version1 and version2.   If version1 > version2 return

LeetCode-面試演算法經典-Java實現002-Add Two Numbers (單鏈表表示的兩個數相加)

原題   You are given two linked lists representing two non-negative numbers. The digits are stor

LeetCode-面試演算法經典-Java實現011-ContainerWithMostWater容納最多的水

原題   Given n non-negative integers a1, a2, …, an, where each represents a point at coordin