当前位置:网站首页>golang 刷leetcode:统计打字方案数
golang 刷leetcode:统计打字方案数
2022-08-02 20:36:00 【用户9710217】
Alice 在给 Bob 用手机打字。数字到字母的 对应 如下图所示。
为了 打出 一个字母,Alice 需要 按 对应字母 i 次,i 是该字母在这个按键上所处的位置。
比方说,为了按出字母 's' ,Alice 需要按 '7' 四次。类似的, Alice 需要按 '5' 两次得到字母 'k' 。
注意,数字 '0' 和 '1' 不映射到任何字母,所以 Alice 不 使用它们。
但是,由于传输的错误,Bob 没有收到 Alice 打字的字母信息,反而收到了 按键的字符串信息 。
比方说,Alice 发出的信息为 "bob" ,Bob 将收到字符串 "2266622" 。
给你一个字符串 pressedKeys ,表示 Bob 收到的字符串,请你返回 Alice 总共可能发出多少种文字信息 。
由于答案可能很大,将它对 109 + 7 取余 后返回。
示例 1:
输入:pressedKeys = "22233"
输出:8
解释:
Alice 可能发出的文字信息包括:
"aaadd", "abdd", "badd", "cdd", "aaae", "abe", "bae" 和 "ce" 。
由于总共有 8 种可能的信息,所以我们返回 8 。
示例 2:
输入:pressedKeys = "222222222222222222222222222222222222"
输出:82876089
解释:
总共有 2082876103 种 Alice 可能发出的文字信息。
由于我们需要将答案对 109 + 7 取余,所以我们返回 2082876103 % (109 + 7) = 82876089 。
提示:
1 <= pressedKeys.length <= 105
pressedKeys 只包含数字 '2' 到 '9' 。
解题思路:
1,看到这个题,很显然可以转化成等价问题:爬楼梯,三种情况
A,如果连续两个字符一样,可以爬1步或2步
B,如果连续三个字符一样,可以爬1步或2步,3步
C,如果连续四个以上字符一样,且是7或9,可以爬1步或2步,3步,4步
E,如果不一样,只能爬一步
2,如此看来就是斐波那契数列问题
3,可以通过动态规划求解
代码实现
func countTexts(pressedKeys string) int {
dp:=make([]int,len(pressedKeys)+1)
dp[0]=1
for i:=1;i<=len(pressedKeys);i++{
if i>=4 && pressedKeys[i-1]==pressedKeys[i-2] && pressedKeys[i-1]==pressedKeys[i-3]&& pressedKeys[i-1]==pressedKeys[i-4] && (pressedKeys[i-1]=='7' || pressedKeys[i-1]=='9'){
dp[i]=(dp[i-1]+dp[i-2]+dp[i-3]+dp[i-4])%1000000007
}else if i>=3 && pressedKeys[i-1]==pressedKeys[i-2] && pressedKeys[i-1]==pressedKeys[i-3]{
dp[i]=(dp[i-1]+dp[i-2]+dp[i-3])%1000000007
} else if i>=2 && pressedKeys[i-1]==pressedKeys[i-2]{
dp[i]=(dp[i-1]+dp[i-2])%1000000007
} else{
dp[i]=dp[i-1]
}
}
return dp[len(pressedKeys)]
}目前是O(n)空间,是否可以继续优化呢?答案是可以到O(1),因为我们最多用到了dp中的四个变量,所以,我们可以用长度为4的数组来优化
func countTexts(pressedKeys string) int {
dp:=[]int{1,1,1,1}
for i:=1;i<=len(pressedKeys);i++{
tmp:=0
if i>=4 && pressedKeys[i-1]==pressedKeys[i-2] && pressedKeys[i-1]==pressedKeys[i-3]&& pressedKeys[i-1]==pressedKeys[i-4] && (pressedKeys[i-1]=='7' || pressedKeys[i-1]=='9'){
tmp=(dp[3]+dp[2]+dp[1]+dp[0])%1000000007
}else if i>=3 && pressedKeys[i-1]==pressedKeys[i-2] && pressedKeys[i-1]==pressedKeys[i-3]{
tmp=(dp[3]+dp[2]+dp[1])%1000000007
} else if i>=2 && pressedKeys[i-1]==pressedKeys[i-2]{
tmp=(dp[3]+dp[2])%1000000007
} else{
tmp=dp[3]
}
dp[0]=dp[1]
dp[1]=dp[2]
dp[2]=dp[3]
dp[3]=tmp
}
return dp[3]
}边栏推荐
- A brief discussion on the transformation of .NET legacy applications
- Nervegrowold hands-on learning deep learning V2 - Bert pre training data set and code implementation
- Informatics Olympiad All-in-One (1257: Knight Moves)
- Informatics orsay a tong (1258: 【 9.2 】 digital pyramid)
- 解道8-编程技术5
- 什么是 IDE
- Wiring diagrams of switches, motors, circuit breakers, thermocouples, and meters
- SublimeText3 安装、配置项、包管理、常用必备插件、常用快捷键以及修改
- Which thread pool does Async use?
- C# Barrier class
猜你喜欢

Flink Yarn Per Job - 创建启动Dispatcher RM JobManager

华为设备配置BFD多跳检测

Bee 事务注解 @Tran 使用实例

56.【全局变量和局部变量专题】

【目标检测】YOLOv5:640与1280分辨率效果对比

【3D视觉】realsense D435三维重建

汉源高科2光12电千兆导轨式网管型工业以太网交换机双光自愈保护式以太网光交换机

【模型压缩】实例分析量化原理

Informatics orsay a tong (1258: 【 9.2 】 digital pyramid)

Use the TCP protocol, we won't lost package?
随机推荐
.NET performance optimization - you should set initial size for collection types
Xcode13.1运行工程报错fatal error: ‘IFlyMSC/IFly.h‘ file not found的问题
《分布式微服务电商》专题(一)-项目简介
【流媒体】推流与拉流简介
【目标检测】YOLOv5:640与1280分辨率效果对比
Likou Question of the Day - Day 46 - 344. Reverse Strings
二叉搜索树的实现
14、学习MySQL 连接的使用
Golang source code analysis: time/rate
Informatics Olympiad All-in-One (1260: [Example 9.4] Intercepting Missiles (Noip1999))
The time series database has been developed for 5 years. What problem does it need to solve?
交 叉 数 组
iframe------------frame-
Tencent YunMeng every jie: I experienced by cloud native authors efficiency best practices case
Use the TCP protocol, we won't lost package?
传感器工作原理
Implement fashion_minst clothing image classification
OP analysis and design
Day35 LeetCode
正则表达式