当前位置:网站首页>【Hot100】17. Letter combination of telephone number
【Hot100】17. Letter combination of telephone number
2022-07-01 16:05:00 【Wang Liuliu's it daily】
17. Letter combination of telephone number
Reference resources :https://leetcode.cn/problems/letter-combinations-of-a-phone-number/solution/tong-su-yi-dong-dong-hua-yan-shi-17-dian-hua-hao-m/
- to flash back
Recursive calls are nested in the loop
There is a small detail in the code implementation , If it is pure string splicing , Many temporary objects will be generated , The performance will be slightly worse ,Java In the implementation, we use StringBuilder Spliced , Tested time consuming 0 millisecond , If it is to use String It takes time for type splicing 6 millisecond .
class Solution {
// A mapping table , The second position is "abc“, The third position is "def"...
// It can also be used here map, Using arrays can save more memory
String[] letter_map = {
" ","*","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
public List<String> letterCombinations(String digits) {
// Pay attention to the boundary conditions
if(digits==null || digits.length()==0) {
return new ArrayList<>();
}
iterStr(digits, new StringBuilder(), 0);
return res;
}
// The final output is list
List<String> res = new ArrayList<>();
// Recursive function
void iterStr(String str, StringBuilder letter, int index) {
// The termination condition of recursion , Note that the termination conditions here look a little different from the dynamic demo , It is mainly optimized
// The dynamic graph is a part of each intercepted string ,"234", become "23", And then become "3", Finally become "", It doesn't perform well
// While using index Record the position of each traversal to the string , It's better
if(index == str.length()) {
res.add(letter.toString());
return;
}
// obtain index The character of position , Suppose the input character is "234"
// The first time you recurs index by 0 therefore c=2, The second time index by 1 therefore c=3, third time c=4
//subString Each time a new string is generated , and index Is to take the current character , So be more efficient
char c = str.charAt(index);
//map_string The following table is from 0 Start all the way to 9, c-'0' You can get the relative array subscript position
// such as c=2 When ,2-'0', Get the subscript as 2,letter_map[2] Namely "abc"
int pos = c - '0';
String map_string = letter_map[pos];
// Traversal string , For example, the first time I got 2, Page is traversal "abc"
for(int i=0;i<map_string.length();i++) {
// Call the next level of recursion , It is difficult to describe in words , Please understand with dynamic diagram
letter.append(map_string.charAt(i));
// If it is String The splicing efficiency of type will be relatively low
//iterStr(str, letter+map_string.charAt(i), index+1);
iterStr(str, letter, index+1);
letter.deleteCharAt(letter.length()-1);
}
}
}
- queue
You can take advantage of the first in, first out feature of queues , Then cooperate with the cycle
class Solution {
public List<String> letterCombinations(String digits) {
if(digits==null || digits.length()==0) {
return new ArrayList<String>();
}
// A mapping table , The second position is "abc“, The third position is "def"...
// It can also be used here map, Using arrays can save more memory
String[] letter_map = {
" ","*","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"
};
List<String> res = new ArrayList<>();
// First, add a null character to the queue
res.add("");
for(int i=0;i<digits.length();i++) {
// Characters traversed by the current , Find the corresponding string in the dictionary table
String letters = letter_map[digits.charAt(i)-'0'];
int size = res.size();
// After calculating the queue length , Take out each element in the queue one by one
for(int j=0;j<size;j++) {
// Take the first element out of the queue every time
String tmp = res.remove(0);
// Then follow "def" Such string splicing , And put it in the queue again
for(int k=0;k<letters.length();k++) {
res.add(tmp+letters.charAt(k));
}
}
}
return res;
}
}
边栏推荐
- The latest NLP game practice summary!
- 新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
- 综述 | 激光与视觉融合SLAM
- 【php毕业设计】基于php+mysql+apache的教材管理系统设计与实现(毕业论文+程序源码)——教材管理系统
- process. env. NODE_ ENV
- Seate中用了shardingjdbc 就不能用全局事务了吗?
- Redis seckill demo
- How to adjust the color of the computer screen and how to change the color of the computer screen
- Summer Challenge harmonyos canvas realize clock
- [daily news]what happened to the corresponding author of latex
猜你喜欢
超视频时代,什么样的技术会成为底座?
AVL 平衡二叉搜索树
毕业后5年,我成为了年薪30w+的测试开发工程师
【LeetCode】43. 字符串相乘
What is the forkjoin framework in the concurrent programming series?
I'm a senior test engineer who has been outsourced by Alibaba and now has an annual salary of 40w+. My two-year career changing experience is sad
IM即时通讯开发万人群聊消息投递方案
The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
Do280 management application deployment - pod scheduling control
One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
随机推荐
The Department came to a Post-00 test paper king who took out 25K. The veteran said it was really dry, but it had been
Automatic, intelligent and visual! Deeply convinced of the eight designs behind sslo scheme
【每日一题】1175. 质数排列
Embedded development: five revision control best practices
Preorder, inorder, follow-up of binary tree (non recursive version)
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
Smart Party Building: faith through time and space | 7.1 dedication
2023届春招实习-个人面试过程和面经分享
二叉树的前序,中序,后续(非递归版本)
Introduction to RT thread env tool (learning notes)
有些能力,是工作中学不来的,看看这篇超过90%同行
Pocket network supports moonbeam and Moonriver RPC layers
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
Some abilities can't be learned from work. Look at this article, more than 90% of peers
ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别
圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
ADS算力芯片的多模型架构研究
The latest NLP game practice summary!
分享在大疆DJI(深圳总部)工作的日常和福利
硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件