全球即时:Leetcode 1006. Clumsy Factorial

The factorial of a positive integer nis the product of all positive integers less than or equal to n.

For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.


【资料图】

We make a clumsy factorial using the integers in decreasing order by swapping out the multiply operations for a fixed rotation of operations with multiply '*', divide '/', add '+', and subtract '-'in this order.

For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.

However, these operations are still applied using the usual order of operations of arithmetic. We do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.

Additionally, the division that we use is floor division such that 10 * 9 / 8 = 90 / 8 = 11.

Given an integer n, return the clumsy factorial of n.

Example 1:

Input: n = 4Output: 7Explanation: 7 = 4 * 3 / 2 + 1

Example 2:

Input: n = 10Output: 12Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1

Constraints:

1 <= n <= 104

class Solution {

public static int clumsy(int n){

int ans=0;

if(n<4){

return fac(n);

}

int k=(n-3)>0?(n-3):0;

ans=fac(n)+k;

// System.out.println(ans);

for (int i = n-4;i>=0; i=i-4) {

int l=(i-3)>0?(i-3):0;

ans=ans-fac(i)+l;

}

return ans;

}

public static int fac(int n){

if(n==0) return 0;

if(n==1) return 1;

if(n==2) return 2;

if(n==3) return 6;

if(n==4) return 6;

return n*(n-1)/(n-2); 

}

}

写个fac的函数,然后依次遍历即可。

Runtime: 2 ms, faster than 70.12% of Java online submissions for Clumsy Factorial.

Memory Usage: 39.4 MB, less than 76.10% of Java online submissions for Clumsy Factorial.

标签: subtract

最近更新

全球即时:Leetcode 1006. Clumsy Factorial
2023-03-01 07:58:35
仪态端庄的意思是什么意思_仪态端庄是什么意思
2023-03-01 06:20:27
明末军队战斗力很弱,降清以后为何个个神勇无比为清朝打下江山?
2023-03-01 03:53:39
一个多月豪掷逾14亿! 中国联塑旗下公司举牌科达制造 此前参与巴安水务定增被深套-环球滚动
2023-02-28 23:56:56
opporeno怎么读音发音_opporeno怎么读
2023-02-28 20:45:56
助力民营企业高质量发展,浦东相关部门积极听需求、优服务、定措施
2023-02-28 18:56:09
泰恩康:“北京泰恩康科技有限公司”与公司不存在任何关联关系
2023-02-28 16:15:48
涂的拼音和组词和部首还有音序_涂的拼音和组词-环球热消息
2023-02-28 15:17:58
悦康药业2022年度净利3.38亿同比减少38.05% 运营成本增加
2023-02-28 14:26:37
2023杭州普通话报名了不想去考试能退费吗 环球即时看
2023-02-28 12:03:09
赵明:荣耀“折叠屏+全能”双旗舰战略在海外正式落地
2023-02-28 10:40:29
教育部课题编号查询_国家教育部课题查询
2023-02-28 09:37:39
毛不易登台高歌嗓音温暖人心 客串车评人开心分享_每日看点
2023-02-28 09:00:52
一品弃仙 废材嫡女狂天下
2023-02-28 04:31:28
《无名》北美扩映、《流浪地球2》闯入北美票房前十,它们出海表现到底如何?
2023-02-28 00:47:56
速递!聚辰股份业绩快报:2022年净利同比增长228%
2023-02-27 21:35:09
环球新动态:浦口城市管理:激活“边角料”资源属性,停车场改造实现新共享
2023-02-27 19:02:41
焦点热讯:万有童书我的第一本石头知识大全
2023-02-27 18:46:29
热门看点:德国的纽伯格林赛道由于大流行而于上个月被迫关闭
2023-02-27 16:56:26
全国人大代表李义虎:大陆将推动国家统一进入快速发展轨道|全球速讯
2023-02-27 15:14:11
当前时讯:上帝之后
2023-02-27 13:33:12
动态:中元股份:公司没有辅助生殖、生育业务
2023-02-27 13:12:23
寿险“开门红”遇寒, 三上市险企发布年度业绩预减公告
2023-02-27 10:59:53
【环球新视野】*ST奇信(002781)2月27日9点36分触及跌停板
2023-02-27 09:49:12
环球即时看!宇智波鼬
2023-02-27 07:42:45
每日快讯!法媒:巴黎后卫金彭贝跟腱断裂,可能伤缺6个月
2023-02-27 06:57:04
全球快资讯丨rsi参数的最佳设置9.20.72_rsi参数最佳设置
2023-02-27 02:00:21
更改iphone运营商名字_iphone改运营商名字
2023-02-26 21:47:07
上海80后夫妻退休,却因300万存款引起争议,评论区两极分化
2023-02-26 18:50:39
东抬头村
2023-02-26 17:26:29