题目:

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].

解题思路:

  看完题目后,我脑子里首先出现的是动态规划算法解决这一类问题。但是仔细想想,又觉得不太对,首先target的范围很大,没有这个大的数组可以保存中间结果。之后脑子里闪过了无数的方法,但都被一一否决了。万般无奈之下,想起了“找规律”的老办法。题目要求是从0开始,第n次操作可以到达target,那么可以先试试找出每次操作可以到达的的number,是否能够发现其中的规律。

function unique(a) {
var res = []; for (var i = 0, len = a.length; i < len; i++) {
var item = a[i]; for (var j = 0, jLen = res.length; j < jLen; j++) {
if (res[j] === item)
break;
} if (j === jLen)
res.push(item);
} return res;
} var reachNumber = function(target) {
var l = [0]
for(var i = 1;i < 6;i++){
var tl = []
while(l.length > 0){
var t = l.pop()
tl.push(t-i)
tl.push(t+i)
}
var tl = unique(tl).sort(function(a,b){
return a-b})
//console.log(i,':',tl[0],tl[1],tl[2],tl[3])
console.log(i,':',tl)
for(var j =0;j<tl.length;j++){
l.push(tl[j])
}
}
}; reachNumber()

输出的结果如下:

1 ':' [ -1, 1 ]
2 ':' [ -3, -1, 1, 3 ]
3 ':' [ -6, -4, -2, 0, 2, 4, 6 ]
4 ':' [ -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10 ]
5 ':' [ -15, -13, -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11, 13, 15 ]

为了控制输出的长度,这里设置了i<6的条件,其实适当把i放大,会发现更明显的规律。

这里就直接把规律列出来了:

a. 第n次操作能到达的最大范围是 -(1+2+...+n)和 (1+2+...+n);

b. 负数的number和正数的number是对称的,可以令target = abs(target);

c. n%4 == 0或者n%4 == 3的时候,只能移动到小于偶数的number;

d. n%4 == 1或者n%4 == 2的时候,只能移动到奇数的number;

所以,要找出最小的n,可以到达abs(target)分两种情况:

1. abs(target)是偶数,需要满足上面的a和c两个条件;

2.abs(target)是奇数,需要满足上面的a和d两个条件;

代码如下:

var reachNumber = function(target) {
if (target < 0){
target = -target
}
if(target == 1 || target == -1){
return 1
}
var isOdd = target%2
var count = 1;
var t = 1;
while(count++) {
t += count
if (t >= target && isOdd == 0 && (count % 4 == 3 || count % 4 == 0)) {
return count;
}
else if (t >= target && isOdd != 0 && (count % 4 == 1 || count % 4 == 2)) {
return count
} }
}; console.log(reachNumber(1))

【leetcode】Reach a Number的更多相关文章

  1. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  2. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  3. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  4. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  5. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  6. 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

  7. 【Leetcode】179. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  8. 【Leetcode】264. Ugly Number II ,丑数

    原题 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime facto ...

  9. 【leetcode】1189. Maximum Number of Balloons

    题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...

随机推荐

  1. win10更新导致chrome打开网页速度太慢

    升级win10之后如果出现chrome内核的浏览器网页总是打不开 打开很慢 而ie和edge是可以正常访问的 用这个方法可以 我弄了几天终于 搞好了 我直接转载过来了 近期,工程师收到大量反馈360浏 ...

  2. git上传项目到github教程

    1 本地下载git 2 进到你项目所在地 3 git init(初始化git仓) 4 git add .(git add <file>将文件添加到git) 5 git commit -m ...

  3. 2019JAVA第四次实验报告

    JAVA实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间 2019/9/29 评分等级 实验四 类的继承 1.实验目的 掌握类的继承方法: 变量的继承和覆盖,方法的继承.重载和 ...

  4. Python 类的私有属性与私有方法

    1.隐藏的使用场景 在Python类中,有些属性和方法只希望在对象的内部被使用,而不希望在外部被访问到, 2.定义方式, 在属性名或方法名前增加两个下划线,定义的就是私有属性或方法 #其实这仅仅这是一 ...

  5. 小记---------网页采集之selenium

    1.元素定位 ID定位元素:  findElement(By.id(“”));  通过元素的名称定位元素:  findElement(By.name(“”));   通过元素的html中的位置定位元素 ...

  6. Git_初步了解

    Git入门篇 一:Git是什么?Git是目前世界上最先进的分布式版本控制系统.工作原理 / 流程: Workspace:工作区Index / Stage:暂存区Repository:仓库区(或本地仓库 ...

  7. gym102215题解

    A Rooms and Passages 题意 给n个数,从起点出发,一直往右走,遇到一个前面出现过其相反数的正数就停下,问对于每个起点都能走多少步. 分析 倒着递推,如果起点是正数,那么肯定可以走, ...

  8. [wpf] collectionViewsource 排序 和分组

    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" xmlns:swd="clr-n ...

  9. SQL语句中*号的缺点

    我觉得这篇博客说的比较好,参考借鉴一下:https://blog.csdn.net/weixin_44588186/article/details/87263756

  10. Vue2 & ElementUI实现管理后台之input获得焦点

    Vue.directive('focus', function (el, option) { var defClass = 'el-input', defTag = 'input'; var valu ...