【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-十大经典排序算法
随机推荐
- Ubuntu新建用户组
新建用户组 sudo addgroup groupname 把现有用户加入新建的用户组 sudo adduser username groupname
- 调试web worker (动态生成的worker)
1.在worker.js源码文件中 写下debugger关键词 2. F12打开控制台,重新刷新页面,加载worker.js文件(注意之前的缓存,chrome推荐使用 ctrl + F5 刷新) 3. ...
- CDH6.3.1安装hue 报错
x 一.查看日志server运行日志 /var/log/cloudera-scm-server/cloudera-scm-server.log 2019-12-11 17:28:34,201 INFO ...
- springBoot03- springboot+jpa+thymeleaf增删改查
参考http://www.mooooc.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 数据库: CREATE TABLE ...
- SpringMvc中乱码问题的解决
一:如果是前台传递的数据有问题. 在tomcat的service.xml中加上: URIEncoding="UTF-8" <Connector URIEncoding=&qu ...
- 【Java】Java引用maven私服jar包及jar包提交私服问题
pom.xml中加入以下配置即可 1.引用私服jar包 <!-- 加载的是 第三方项目使用的jar包 --> <repositories> <repository> ...
- python使用开源图片识别第三方库tesseract
详细安装博客:https://blog.csdn.net/luanyongli/article/details/81385284 第一步tesseract-ocr的安装如果不会请参照:https:// ...
- [CSP-S模拟测试50]反思+题解
??大部分人都觉得T3是道不可做题去刚T1T2了,于是我就侥幸苟到了前面? 这场考试比较成功的就是快速水掉了T1T2的部分分,1h拿到88分起码为之后硬肝T3上了保险(赛后发现就算T3爆零也能rank ...
- 实验四 《Android程序设计》
实验四 <Android程序设计实验报告>-20175131王泽龙 实验四 Android程序设计-1 ①实验要求: Android Stuidio的安装测试:参考<Java和And ...
- servlet3.0 异步处理
转:https://blog.csdn.net/benjamin_whx/article/details/38874657 13.1.概述 计算机的内存是有限的.Servlet/JSP容器的设计者很清 ...