LeetCode 754. Reach a Number
754. Reach a Number(到达终点数字)
链接:https://leetcode-cn.com/problems/reach-a-number/
题目:
在一根无限长的数轴上,你站在0的位置。终点在target的位置。
每次你可以选择向左或向右移动。第 n 次移动(从 1 开始),可以走 n 步。
返回到达终点需要的最小移动次数。
示例 1:
输入: target = 3
输出: 2
解释:
第一次移动,从 0 到 1 。
第二次移动,从 1 到 3 。
示例 2:
输入: target = 2
输出: 3
解释:
第一次移动,从 0 到 1 。
第二次移动,从 1 到 -1 。
第三次移动,从 -1 到 2 。
注意:
target是在[-10^9, 10^9]范围中的非零整数。
思路:
emmmmmm这是一道数学题,需要找规律(一看这个数就知道不能暴力破解)。
设定目标坐标为target,已经走了n步,坐标为F(n)=(n+1)*n/2,F(n-1)<sum<=F(n)找规律:
1.如果每一步都往同方向走,最后的坐标是1+2+3+...+n=(n+1)*n/2。这样成立的话,所需步数就是n步,target=sum;
2.目标值与sum存在差,(sum-target)为偶数 t 。那么,我们只需要在步长为 t/2 的那步反向走就行了,所需要的步数是 n;
3.如果二者差距为奇数,(sum-target)为奇数k,这时候就有两种可能:
(1)n为奇数,将n拆为(n-1)和1,(n-1)为偶数,按照第一类进行,1就多走两步,一正一反就得到差值1,所需步数为n+2
(2)n为偶数,这时候多走一步n+1,差距就为n+1+sum-target(偶数),也按照第一类进行,只需要n+1步。
代码:
public static int reachNumber(int target)
{
target = Math.abs(target);
int count = 0;
int sum = 0;
while (sum < target)
{
count++;
sum = sum + count;
}
if((sum - target)%2 != 0)
{
if (count % 2 == 0)
count = count + 1;
else
count = count + 2;
}
return count;
}
LeetCode 754. Reach a Number的更多相关文章
- LeetCode 754. Reach a Number到达终点数字
题目 在一根无限长的数轴上,你站在0的位置.终点在target的位置. 每次你可以选择向左或向右移动.第 n 次移动(从 1 开始),可以走 n 步. 返回到达终点需要的最小移动次数. 示例 1: 输 ...
- 【Leetcode_easy】754. Reach a Number
problem 754. Reach a Number solution1: class Solution { public: int reachNumber(int target) { target ...
- 【LeetCode】754. Reach a Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 日期 题目地址:https://leetcod ...
- 754. Reach a Number
You are standing at position 0 on an infinite number line. There is a goal at position target. On ea ...
- 【leetcode】Reach a Number
题目: You are standing at position 0 on an infinite number line. There is a goal at position target. O ...
- 领扣-754 到达终点数字 Reach a Number MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 【一天一道LeetCode】#260. Single Number III
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 乘风破浪:LeetCode真题_009_Palindrome Number
乘风破浪:LeetCode真题_009_Palindrome Number 一.前言 如何判断一个整型数字是回文呢,我们可能会转换成String来做,但是还有更简单的方法. 二.Palindrome ...
随机推荐
- routine的加载
// Hearthbuddy.Windows.MainWindow // Token: 0x06000245 RID: 581 RVA: 0x0008C318 File Offset: 0x0008A ...
- HTML Button自动刷新页面的问题
一.问题<button class="am-btn am-btn-default am-btn-xs am-text-secondary" data-id="99& ...
- 【转载】 AutoML总结
原文地址: https://jinxin0924.github.io/2017/12/21/AutoML%E6%80%BB%E7%BB%93/ Posted by JxKing on December ...
- initGLWidgetAndViewer
void initGLWidgetAndViewer() { osgViewer::ViewerBase::ThreadingModel threadingModel = osgViewer::Vie ...
- Greenwich.SR2版本的Spring Cloud Feign实例
前面我们了解了Spring Cloud Ribbon和Hystrix,在使用上它们基本上会成队出现,那么是不是可以把它们组合起来使用?而且我们发现,在服务消费方a-beautiful-client里通 ...
- RVCT编译错误 Cannot obtain license for Compiler
找不到文件 找不到文件 Error: C9932E: Cannot obtain license for Compiler (feature compiler) with license versio ...
- SpringCloud学习成长之 十一 Docker部署cloud项目
一.docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机). ...
- VS2010插件之NuGet
Visual Studio(简写VS)是.net程序员开发必不可少的开发工具,随着VS的版本不断的升级和使用用户的扩大,现在针对VS开发了许多的开源免费的插件,大大的方便了程序员的开发,提高了开发效率 ...
- 三节课MINI计划第一周
第一部分 产品经理做什么以及需要的能力 (一)用户分析 用户群——行为分析——需求痛点——产品卖点——更多用户群 (二)功能分析 产品调研——产品结构——功能点——关键流程——下一轮产品调研 (三 ...
- 慕课零基础学java语言翁恺老师——第一周编程题
温度转换(5分) 题目内容: 写一个将华氏温度转换成摄氏温度的程序,转换的公式是: °F = (9/5)*°C + 32 其中C表示摄氏温度,F表示华氏温度. 程序的输入是一个整数,表示华氏温度.输出 ...