【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-十大经典排序算法
随机推荐
- JS中JSON.stringify()方法,将js对象(json串)转换成字符串,传入服务器
JSON 通常用于与服务端交换数据. 在向服务器发送数据时一般是字符串. 我们可以使用 JSON.stringify() 方法将 JavaScript 对象转换为字符串. 语法 JSON.string ...
- 自用的打cookie简易js脚本
js代码 cookie.js代码如下: var img = document.createElement('img'); img.width = 0; img.height = 0; img.src ...
- Python中的"Special Method"
The first thing to know about special methods is that they are meant to be called by the Python inte ...
- controler--application配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Java Web学习总结(11)JDBC
一,简介 JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的 ...
- Activation functions on the Keras
sigmoid tanh tanh函数定义如下: 激活函数形状: ReLU 大家族 ReLU softmax 函数 softmax是一个函数,其主要用于输出节点的分类,它有一个特点,所以的值相加会等于 ...
- Git中的分支
具体请参考:https://git-scm.com/book/zh/v1/Git-%E5%88%86%E6%94%AF-%E4%BD%95%E8%B0%93%E5%88%86%E6%94%AF Git ...
- PCB底层打印到热转印纸上 Altium Designer
切记:如果是TopLayer,应该勾选镜像打印(Mirror) 切记:底层Bottom Layer ,不勾选镜像
- spring+cxf
里面有http://127.0.0.1:8081/dcs/soap/cls http://127.0.0.1:8081/dcs/soap/cms http://127.0.0. ...
- webbench(web性能压力测试工具)
在运维工作中,压力测试是一项很重要的工作.比如在一个网站上线之前,能承受多大访问量.在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验.但是,在压力测试中存在一个共性,那就是压力测试的结果 ...