最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Leetcode 1006. Clumsy Factorial

2023-03-01 08:14 作者:您是打尖兒還是住店呢  | 我要投稿

The?factorial?of a positive integer?n?is 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的函數(shù),然后依次遍歷即可。

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.


Leetcode 1006. Clumsy Factorial的評論 (共 條)

分享到微博請遵守國家法律
长岭县| 黄骅市| 中宁县| 道孚县| 藁城市| 乌兰察布市| 肇源县| 长子县| 桃园县| 凤庆县| 宁安市| 青河县| 徐闻县| 临澧县| 赤壁市| 澄江县| 大荔县| 安康市| 芷江| 深水埗区| 丰原市| 上杭县| 东兴市| 贡山| 扎兰屯市| 云龙县| 昌平区| 九江市| 台北市| 新龙县| 勃利县| 汕尾市| 临邑县| 信宜市| 宜都市| 松原市| 屯留县| 太仆寺旗| 平泉县| 崇州市| 永丰县|