【leetcode】1006. Clumsy Factorial
题目如下:
Normally, the factorial of a positive integer
nis 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 / 8equals11. This guarantees the result is an integer.
Implement the clumsyfunction 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-十大经典排序算法
随机推荐
- SpringBoot JSON文件读取
@Componentpublic class StepExecutor implements Runnable { @Value("classpath:menu.json") pr ...
- 【锁】synchronized的实现(偏向锁、轻量级锁、重量级锁)
synchronized的三种应用方式 一. 修饰实例方法,作用于当前实例加锁,进入同步代码前要获得当前实例的锁. 二. 修饰静态方法,作用于当前类对象加锁,进入同步代码前要获得当前类对象的锁. 三. ...
- spring-boot整合Mybatis案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 3.2.5 2.Maven Plugin管理 <?xml version="1.0&q ...
- 【UNR #2】黎明前的巧克力 解题报告
[UNR #2]黎明前的巧克力 首先可以发现,等价于求 xor 和为 \(0\) 的集合个数,每个集合的划分方案数为 \(2^{|S|}\) ,其中 \(|S|\) 为集合的大小 然后可以得到一个朴素 ...
- AGC024B Backfront
题目大意 给你一个1~n的排列 你有两个操作:将一个数移到最后或将一个数移到最前 问将排列排序最少要几次操作 分析 年纪大了,脑子不行了.. 实际我们只需求出对与一段连续的数它在排列中已经有序的最长长 ...
- inline-block,inline,block,table-cell,float
float:left ---------------------------------------------------------------------------------------- ...
- linux在二进制文件中查找pattern的offset
参考:http://stackoverflow.com/questions/14141008/grep-offset-of-ascii-string-from-binary-file strings ...
- 【awk】 处理多个文件
处理多个文件: 1. 可以在代码中指定读取某个文件, 其他的用命令行输入 while ( geline < "file.txt" > 0 ) { ...
- 牛客网练习赛 2 烟花(概率dp)
题目传送门 烟花 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K Special Judge, 64bit IO Format: %lld 题目 ...
- Java关于线程池的使用
一.四种线程池创建的方式 Java通过Executors提供四种线程池,分别为: newCachedThreadPool 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回 ...