Leetcode 1006. 笨阶乘
- 用户通过次数305
- 用户尝试次数347
- 通过次数309
- 提交次数665
- 题目难度Medium
通常,正整数 n
的阶乘是所有小于或等于 n
的正整数的乘积。例如,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
。
相反,我们设计了一个笨阶乘 clumsy
:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)。
例如,clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
。然而,这些运算仍然使用通常的算术运算顺序:我们在任何加、减步骤之前执行所有的乘法和除法步骤,并且按从左到右处理乘法和除法步骤。
另外,我们使用的除法是地板除法(floor division),所以 10 * 9 / 8
等于 11
。这保证结果是一个整数。
实现上面定义的笨函数:给定一个整数 N
,它返回 N
的笨阶乘。
示例 1:
输入:4
输出:7
解释:7 = 4 * 3 / 2 + 1
示例 2:
输入:10
输出:12
解释:12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
提示:
1 <= N <= 10000
-2^31 <= answer <= 2^31 - 1
class Solution {
public:
int clumsy(int N) {
if(N == ) return ;
if(N == ) return ;
if(N == ) return ;
if(N == ) return ;
int qianyikuai = INT_MAX;
int qianyige = N;
int j = ;
for(int i=N-;i > ;i--,j++){
if(j% == )qianyige = qianyige*i;
if(j% == )qianyige = qianyige/i;
if(j% == ){
if(j == )qianyige += i;
else qianyige -= i;
}
if(j% == ){
if(qianyikuai == INT_MAX) {qianyikuai = qianyige;qianyige = i;}
else{
qianyikuai -= qianyige;
qianyige = i;
}
}
}
return qianyikuai - qianyige;
}
};
__又是自己写的屎山
class Solution {
public:
int clumsy(int N) {
int rate = N;
int ans = ;
int op = ;
for(int i=N-;i > ;i--){
if(op == ) rate*=i;
if(op == ) rate/=i;
if(op == ) {ans += rate+i;rate = ;} //这里ans加过rate了,就把rate置0了
if(op == ) rate = -i;
op = (op+)%;
}
ans += rate;
return ans;
}
};
_大佬写的,哭了
Leetcode 1006. 笨阶乘的更多相关文章
- 【python】Leetcode每日一题-笨阶乘
[python]Leetcode每日一题-笨阶乘 [题目描述] 通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积.例如,factorial(10) = 10 * 9 * 8 * 7 * 6 ...
- LeetCode竞赛题:笨阶乘(我们设计了一个笨阶乘 clumsy:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)。)
通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积.例如,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.相反,我们设计了一个笨 ...
- [Swift]LeetCode1006. 笨阶乘 | Clumsy Factorial
Normally, the factorial of a positive integer n is the product of all positive integers less than or ...
- 每日一道 LeetCode (41):阶乘后的零
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- LeetCode 172:阶乘后的零
给定一个整数 n, 返回 n! 结果中尾数为零的数量. 示例 : 输入: 输出: 解释: ! = , 尾数中没有零. 示例 : 输入: 输出: 解释: ! = , 尾数中有个零. 说明:算法的时间复杂 ...
- Leetcode 1006. Clumsy Factorial
class Solution(object): def clumsy(self, N): """ :type N: int :rtype: int "" ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 「Leetcode-算法_MId1006」从单栈到双栈
Mid 1006 笨阶乘 栈/后缀 运算优化 + 栈 思路描述 每四个数一组 这四个数中前三个会进行乘.除 然后与最后一个相加 Stack 入前三个之和 与 最后一个数 以 4 举例 运算式 4 * ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
随机推荐
- jlink的SWD与JTAG下载模式的对应接线方法
参考博客:http://blog.csdn.net/qq_26093511/article/details/59484249 (1)如果用jtag模式下载的话需要接线: jlink ...
- 使用python+ffmpeg+youtube-dl下载youtube上的视频
一.准备工作 1.安装python,详见https://www.cnblogs.com/cnwuchao/p/10562416.html 2.安装ffmpeg,详见https://www.cnblog ...
- java复制文件夹中的所有文件和文件夹到另一个文件夹中
1.复制文件夹 public static void copyDir(String oldPath, String newPath) throws IOException { File file = ...
- 【Django】【环境配置】Mac
mysql环境配置:http://www.cnblogs.com/chenmo-xpw/p/6102933.html
- centos7 查看防火墙状态
[root@xxx ~]# firewall-cmd --state not running
- Oracel中的NVL函数
Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...
- springboot学习之授权Spring Security
SpringSecurity核心功能:认证.授权.攻击防护(防止伪造身份) 涉及的依赖如下: <dependency> <groupId>org.springframework ...
- vue自定义错误界面
方案一: 当输入错误链接错误或者找不到页面,在router里可以定义一个404页面,具体可以这样做:在routes里面这样写: { path:'*', component:error, name:'e ...
- [osg][opengl]透视投影的参数Perspective
gluPerspective这个函数指定了观察的视景体(frustum为锥台的意思,通常译为视景体)在世界坐标系中的具体大小,一般而言,其中的参数aspect应该与窗口的宽高比大小相同.比如说,asp ...
- FileNotFoundException报错, src\main\....\....(拒绝访问) , 原因:1. 方法没有判断文件夹和文件, 2.没有指明文件的具体路径和名字