【LeetCode】754. Reach a Number 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/reach-a-number/description/
题目描述
You are standing at position 0 on an infinite number line. There is a goal at position target.
On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps.
Return the minimum number of steps required to reach the destination.
Example 1:
Input: target = 3
Output: 2
Explanation:
On the first move we step from 0 to 1.
On the second step we step from 1 to 3.
Example 2:
Input: target = 2
Output: 3
Explanation:
On the first move we step from 0 to 1.
On the second move we step from 1 to -1.
On the third move we step from -1 to 2.
Note:
- target will be a non-zero integer in the range [-10^9, 10^9].
题目大意
每次走的步数是增加1步,方向是可以向左或者向右,求通过多少步之后能到达target。
解题方法
数学
非常不喜欢数学题,所以花花酱和Grandyang大神的帖子粘在这里了。
花花酱:https://zxi.mytechroad.com/blog/math/leetcode-754-reach-a-number/
Grandyang大神:http://www.cnblogs.com/grandyang/p/8456022.html
class Solution(object):
def reachNumber(self, target):
"""
:type target: int
:rtype: int
"""
target = abs(target)
k = 0
sum = 0
while sum < target:
k += 1
sum += k
d = sum - target
if d % 2 == 0:
return k
return k + 1 + (k % 2)
C++版本如下:
class Solution {
public:
int reachNumber(int target) {
target = abs(target);
int k = 0;
int sum = 0;
while (sum < target) {
sum += (++k);
}
const int d = sum - target;
if (d % 2 == 0) return k;
return k + 1 + (k % 2);
}
};
日期
2018 年 11 月 26 日 —— 11月最后一周!
【LeetCode】754. Reach a Number 解题报告(Python & C++)的更多相关文章
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- LeetCode 754. Reach a Number
754. Reach a Number(到达终点数字) 链接:https://leetcode-cn.com/problems/reach-a-number/ 题目: 在一根无限长的数轴上,你站在0的 ...
- 【LeetCode】263. Ugly Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 除去2,3,5因子 日期 [LeetCode] 题目 ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
- 【LeetCode】509. Fibonacci Number 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
随机推荐
- FASTA/Q序列处理神器---seqkit
该软件对于处理FASTA/Q十分方便,省去自己编写脚本 安装 1 conda install seqkit 使用 序列操作(seq) 1 ## 取方向序列 2 seqkit seq test.fa - ...
- BaiduPCS-Go----百度云下载工具
1.网页登录百度网盘:https://pan.baidu.com/2.百度输入法生成:http://pcs.baidu.com/rest/2.0/pcs/file?app_id=265486& ...
- iptables_超解
查询: -t选项,指定要操作的表,使用-L选项,查看-t选项对应的表的规则,-L选项的意思是,列出规则,所以,上述命令的含义为列出filter表的所有规则 显示出了3条链INPUT链.FORWARD链 ...
- linux—查看所有的账号以及管理账号
用过Linux系统的人都知道,Linux系统查看用户不是会Windows那样,鼠标右键看我的电脑属性,然后看计算机用户和组即可. 那么Linux操作系统里查看所有用户该怎么办呢?用命令.其实用命令就能 ...
- Spark3学习入门【基于Java】
Spark 是离线数据处理的一种大数据技术,和Flick相比数据处理要延后,因为Flick是实时数据处理,而Spark需要先读取数据到内存. Spark的库是基于Scala写的,虽然Scala也是运行 ...
- Linux内存管理和寻址详解
1.概念 内存管理模式 段式:内存分为了多段,每段都是连续的内存,不同的段对应不用的用途.每个段的大小都不是统一的,会导致内存碎片和内存交换效率低的问题. 页式:内存划分为多个内存页进行管理,如在 L ...
- 哪里可以下载支付宝demo或者sdk
http://club.alipay.com/read-htm-tid-9976972.html 这里有所有的demo和sdk包括移动产品的demo.在他的论坛里面呢 真心恶心啊.不放到主页.
- ubantu上编辑windows程序
命令简记 cd $GOROOT/src cp -r $GOROOT /root/go1.4 CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash 操作 ...
- EM配置问题
配置EM,首先要保证dbconsole在运行. C:\Users\dingqi>emctl start dbconsoleEnvironment variable ORACLE_UNQNAME ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(7. 安装Docker私有镜像仓库)
1. 简介 1.1. 自己做个私有镜像,方便上传和下载,我也在docker官网注册了一个账号,做好的镜像可以传上去 1.2. Redhat自带私有镜像的功能,需要安装包,这是howto: https: ...