https://leetcode.com/problems/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 exactlyn'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to getn'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'.Note:
- The
nwill be in the range [1, 1000].
分解素因数
class Solution {
public:
//分解素因数
int minSteps(int n) {
int result = 0;
int cur = 2;
while(cur < n/2){
if(n % cur == 0){
result += cur;
n /= cur;
}else{
++cur;
}
}
if(n != 1) result += n;
return result;
}
};
博客围绕LeetCode上的两键键盘问题展开,初始记事本只有字符'A',可进行复制和粘贴操作,需用最少步骤得到指定数量的'A',还提及该问题可通过分解素因数解决,指定数字范围在[1, 1000]。
1885

被折叠的 条评论
为什么被折叠?



