【LeetCode】1079. Letter Tile Possibilities 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/letter-tile-possibilities/
题目描述
You have a set of tiles, where each tile has one letter tiles[i] printed on it. Return the number of possible non-empty sequences of letters you can make.
Example 1:
Input: "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
Example 2:
Input: "AAABBC"
Output: 188
Note:
1 <= tiles.length <= 7- tiles consists of uppercase English letters.
题目大意
给出了一些活字印刷的模具,看能组合成多少种不同的单词。
解题方法
回溯
又是一个经典的回溯法。
让我们先看下AAB能构成的结果:
A // 剩余A、B
AA AB // 剩余B,剩余A
AAB ABA // 不剩
B // 剩余A、A
BA // 剩余A
BAA // 不剩
我们可以发现,先选择一个字母开始,然后从剩余的字母里选择1个、2个…直至用完所有字母,放到了第一个字母的后面。
因此是一个递归的方法,先统计每个字母出现的多少次,然后从中选择一个字母,再从剩下的字母中选择,直至所有字母都用完为止。
这里回溯的含义是,我们使用了个数组保存每个字母出现的次数,每次拼接上一个新的字母的时候,把结果res就加一,同时把对应的字母出现的次数减一;当把剩余的结果递归完成之后,需要把当前的字母的次数加上,以便后续的遍历,故称为回溯。
那为什么使用统计字母出现的次数,而不是直接在原来的单词上选择呢?好处是,这样同样的字母在同样的位置只会被选择一次。比如AAB的第一个A和第二个A都可以组成AB,如果在单词上选可能需要set进行去重,但是统计字母出现的次数的时候,在第一个位置选择A的时候只会选择一次。
C++代码如下:
class Solution {
public:
int numTilePossibilities(string tiles) {
vector<int> count(26, 0);
for (char c : tiles) {
count[c - 'A'] ++;
}
int res = 0;
backtrack(count, res);
return res;
}
void backtrack(vector<int>& count, int& res) {
for (int i = 0; i < 26; ++i) {
if (count[i] == 0) continue;
res ++;
count[i] --;
backtrack(count, res);
count[i] ++;
}
}
};
日期
2019 年 9 月 25 日 —— 做梦都在秋招,这个秋天有毒
【LeetCode】1079. Letter Tile Possibilities 解题报告 (C++)的更多相关文章
- LeetCode 1079. Letter Tile Possibilities
原题链接在这里:https://leetcode.com/problems/letter-tile-possibilities/ 题目: You have a set of tiles, where ...
- 【leetcode】1079. Letter Tile Possibilities
题目如下: You have a set of tiles, where each tile has one letter tiles[i]printed on it. Return the num ...
- LeetCode 784 Letter Case Permutation 解题报告
题目要求 Given a string S, we can transform every letter individually to be lowercase or uppercase to cr ...
- 【LeetCode】848. Shifting Letters 解题报告(Python)
[LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
随机推荐
- [R] venn.diagram保存pdf格式文件?
vennDiagram包中的主函数绘图时,好像不直接支持PDF格式文件: dat = list(a = group_out[[1]][,1],b = group_out[[2]][,1]) names ...
- Go语言缺陷
我为什么放弃Go语言 目录(?)[+] 我为什么放弃Go语言 有好几次,当我想起来的时候,总是会问自己:我为什么要放弃Go语言?这个决定是正确的吗?是明智和理性的吗?其实我一直在认真思考这个问题. 开 ...
- 用C语言的LED实验,有汇编哦!
C语言LED实验 1.汇编激活CPU 首先要明白对于没有系统开发板(也就是裸机)来说,是没办法直接对C进行识别.所以需要一段汇编语言,来配置CPU的资源,选择CPU运行模式,初始化指针位置. 代码如下 ...
- MapReduce08 数据清洗(ETL)和压缩
目录 数据清洗(ETL) ETL清洗案例 需求 需求分析 实现代码 编写WebLogMapper类 编写WebLogDriver类 打包到集群运行 压缩 概念 MR支持的压缩编码 压缩算法对比 压缩性 ...
- python 从ubantu环境迁移到windows环境
下载安装Anaconda3 Anaconda3-2021.05-Windows-x86_64.exe 默认安装目录 C:\ProgramData\Anaconda3 可以启动Anaconda查看不同的 ...
- linux之sar命令详解
sar(System Activity Reporter系统活动情况报告)是目前Linux上最为全面的系统性能分析工具之一,可以从多个方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情况 ...
- Oracle 创建 md5 加密函数
使用 Oracle 的 utl_raw.DBMS_OBFUSCATION_TOOLKIT 可以获取 md5 加密字符串: select utl_raw.cast_to_raw(DBMS_OBFUSCA ...
- 应用springMVC时如果配置URL映射时如下配置
应用springMVC时如果配置URL映射时如下配置 [html] view plaincopy<servlet> <servlet-name>appServlet</s ...
- 数据源(Data Source
数据源(Data Source)顾名思义,数据的来源,是提供某种所需要数据的器件或原始媒体.在数据源中存储了所有建立数据库连接的信息.就像通过指定文件名称可以在文件系统中找到文件一样,通过提供正确的数 ...
- Spring.DM版HelloWorld
本文主要描述使用Spring.DM2.0,创建OSGi的HelloWorld演示程序,理解Spring.DM的OSGi框架实现机制. 环境描述: 项目 版本 Eclipse 3.7.x JDK 1 ...