【leetcode】1006. Clumsy Factorial
题目如下:
Normally, the factorial of a positive integer
n
is the product of all positive integers less than or equal ton
. For example,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
.We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: 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
equals11
. This guarantees the result is an integer.
Implement the clumsy
function as defined above: given an integerN
, it returns the clumsy factorial ofN
.Example 1:
Input: 4
Output: 7
Explanation: 7 = 4 * 3 / 2 + 1Example 2:
Input: 10
Output: 12
Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1Note:
1 <= N <= 10000
-2^31 <= answer <= 2^31 - 1
(The answer is guaranteed to fit within a 32-bit integer.)
解题思路:把等式拆分成两部分,一是N*(N-1)/(N-2),二是加上N+3。
代码如下:
class Solution(object):
def clumsy(self, N):
"""
:type N: int
:rtype: int
"""
add = 0
other = None
while N > 0:
tmp = N
if N - 1 > 0:
tmp *= (N-1)
if N - 2 > 0:
tmp /= (N-2)
if N - 3 > 0:
add += (N-3)
if other == None:
other = tmp
else:
other -= tmp
N -= 4
return other + add
【leetcode】1006. Clumsy Factorial的更多相关文章
- 【LeetCode】1006. Clumsy Factorial 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接eval 日期 题目地址:https://lee ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- iPython清屏命令
!clear for Unix-like systems !CLS for Windows
- pylab和pyplot的区别
Pylab combines the functionality of pyplot with the capabilities of NumPy in a single namespace, and ...
- Xcode7.1环境下上架iOS App到AppStore 流程②
前言部分 part二部分主要讲解 iOS App IDs 的创建.概要文件的配置.以及概要文件安装的过程. 一.iOS App IDs 的创建 1)进入如图1所示界面点击右上角箭头所指的加号 进入iO ...
- 网络体系之TCP/IP模型
TCP/IP参考模型是因特网使用的参考模型,这个体系结构在它的两个主要协议出现以后,被称为TCP/IP参考模型.该模型将网络协议分为四层:网络接口层.网络层.运输层.应用层. TCP/IP协议不是TC ...
- delphi 判断MDI窗体的子窗体是否存在
//***************************************************************************//函 数名: CreateForm //返 ...
- HihoCoder 1055 刷油漆 (树上背包)
题目:https://vjudge.net/contest/323605#problem/A 题意:一棵树,让你选择m个点的一个连通块,使得得到的权值最大 思路:树上背包,我们用一个dp数组,dp[i ...
- jmeter 导入csv数据中json格式数据取值不完整
1.jmeter中添加csv数据文件时,数据是json格式 2.jmeter中执行取值发现只取了一部分 分析原因,json格式数据,中间有逗号,而csv是根据逗号来分割的,这回导致我们取值错位. 解决 ...
- 测开之路二十六:Flask基础之最小web程序
Flask中文文档:http://docs.jinkan.org/docs/flask/ 安装Flask库 选端口号的一种方法(避免和别人选的端口冲突,小于1024的时候重新选) 最小web程序 用1 ...
- MFC坐标问题
页面空间中的矩形被称为窗口,设备空间中的矩形被称为视口. 页面空间与设备空间的转换示意图: 页面空间到设备空间的转换需要两个矩形的宽高比(转换因子). 设备空间到物理空间转换的唯一作用是平移,并由Wi ...
- MSF——Meterpreter(三)
MSF系列: MSF——基本使用和Exploit模块(一) MSF——Payload模块(二) MSF——Meterpreter(三) MSF——信息收集(四) 一.简介 Meterpreter是Me ...