650. 2 Keys Keyboard复制粘贴的次数
[抄题]:
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:
Copy All
: You can copy all the characters present on the notepad (partial copy is not allowed).Paste
: You can paste the characters which are copied last time.
Given a number n
. You have to get exactly n
'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n
'A'.
Example 1:
Input: 3
Output: 3
Explanation:
Intitally, we have one character 'A'.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get 'AA'.
In step 3, we use Paste operation to get 'AAA'.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为是dp数组,结果只需要稍微除一下就行了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- totalSteps += singleSteps; 需要一直相加来积累一次性能走的最长步数,否则要走很多次
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
需要while循环一直相加的时候要注意点
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int minSteps(int n) {
//initialization: totalSteps = 0
int totalSteps = 0; //for loop for n
for (int singleSteps = 2; singleSteps <= n; singleSteps++) {
while (n % singleSteps == 0) {
totalSteps += singleSteps;
n /= singleSteps;
}
} return totalSteps; /*
singleSteps 2 2
totalSteps 2 4
n 2 1
*/
}
}
650. 2 Keys Keyboard复制粘贴的次数的更多相关文章
- 651. 4 Keys Keyboard复制粘贴获得的最大长度
[抄题]: Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on scre ...
- [LeetCode] 650. 2 Keys Keyboard 两键的键盘
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- [leetcode] 650. 2 Keys Keyboard (Medium)
解法一: 暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历. 注意剪枝的地方: 1.当前A数量大于目标数量,停止搜索 2.当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态. Runtim ...
- 【LeetCode】650. 2 Keys Keyboard 只有两个键的键盘(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 素数分解 日期 题目地址:https://le ...
- LC 650. 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- LeetCode 650 - 2 Keys Keyboard
LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...
- 650. 2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- [LeetCode] 651. 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
- [LeetCode] 4 Keys Keyboard 四键的键盘
Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...
随机推荐
- 1_2_3_4_5 Html-Css
--------------------------------------------------------------------------------- HTML骨架 <HTML> ...
- java android 捕获未处理异常
1. 定义一个异常处理类 public class ExceptionHandler implements Thread.UncaughtExceptionHandler { public Excep ...
- linux下insmod模块出现“Invalid parameters"
在编译一个模块时,会出现WARNING:"函数名" undefined!,这 说明该模块所依赖的模块还没有加载进内核,需要先加载所依赖的模块. 当加载依赖模块后,使用insmod会 ...
- centos7.1安装tomcat8
上传软件包 [root@linux-node1 tools]# ls apache-tomcat-8.0.32.tar.gz jdk-8u74-linux-x64.tar.gz 添加普通用户tomca ...
- win10+vs2015编译caffe的cpu debug版本、部署matcaffe
一.编译caffe 1.安装python-3.5.2-amd64.exe https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe ...
- [ZZ] 基于Matlab的标记分水岭分割算法
基于Matlab的标记分水岭分割算法 http://blog.sina.com.cn/s/blog_725866260100rz7x.html 1 综述 Separating touching obj ...
- 关于缓冲的认识---Frame Buffer
关于缓冲的认识---Frame Buffer 重点来了:
- Excel函数之rank应用
该函数的功能就是对现有数据指标进行排名 示例:对产品进行销售总额的排名 首先要知道排名需要用到rank函数 number参数就是你要进行排名的数据 ref参数就是该指标需要在哪个区域内进行比较定位排名 ...
- Nuke Python module的使用
最近很多脚本工作都需要脱离nuke的gui环境运行,没有了script editor就必须要尝试Nuke Python module功能了.该模式可以执行大部分在GUI环境中的命令,在自动生成或者批量 ...
- UDP广播包
一,广播地址: 广播地址是专门用于同时向网络中所有工作站进行发送的一个地址.在使用TCP/IP 协议的网络中,主机号为全1的IP地址为广播地址.例如,对于 :192.168.199.0(掩码:255. ...