【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 ...
随机推荐
- Python3编译安装ssl模块问题
本文以Centos系统为例 1.确保linux系统中安装了ssl-devel包 2.编译安装ssl模块到Python3中 1.查看linux系统中是否安装了ssl-devel包 # 查看命令 rpm ...
- 『学了就忘』Linux文件系统管理 — 62、手动分配swap分区
目录 1.查看swap分区情况 2.手动修改swap分区 3.格式化swap分区 4.使用swap分区 5.配置swap分区开机之后自动挂载 1.查看swap分区情况 swap分区就相当于是内存的一个 ...
- 点击下拉选择触发事件【c#】
<asp:DropDownList ID="ddlRegionList" runat="server" AutoPostBack="true&q ...
- 6 — springboot中设置默认首页 -没屁用
1.页面在static目录中时 2).测试 2.页面在templates模板引擎中时 1).这种需要导入相应的启动器 <dependency> <groupId>org.spr ...
- 扩展kmp 学习笔记
学习了一下这个较为冷门的知识,由于从日报开始看起,还是比较绕的-- 首先定义 \(Z\) 函数表示后缀 \(i\) 与整个串的 \(lcp\) 长度 一个比较好的理解于实现方式是类似于 \(manac ...
- 为构建大型复杂系统而生的微服务框架 Erda Infra
作者|宋瑞国(尘醉) 来源|尔达 Erda 公众号 导读:Erda Infra 微服务框架是从 Erda 项目演进而来,并且完全开源.Erda 基于 Erda Infra 框架完成了大型复杂项目的 ...
- academy
academy at/in school都行,academy一般用at. The word comes from the Academy in ancient Greece, which derive ...
- Kafka入门教程(二)
转自:https://blog.csdn.net/yuan_xw/article/details/79188061 Kafka集群环境安装 相关下载 JDK要求1.8版本以上. JDK安装教程:htt ...
- 生产环境高可用centos7 安装配置RocketMQ-双主双从-同步双写(2m-2s-sync)
添加hosts信息[四台机器] vim /etc/hosts 192.168.119.130 rocketmq-nameserver1 192.168.119.130 rocketmq-master1 ...
- Docker学习(一)——安装docker
Suse12上安装docker 对于suse13.2之后的版本,因为docker已经被添加到了suse仓库中,直接使用sudo zypper install docker即可. suse12不 ...