【leetcode】Reach a Number
题目:
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的更多相关文章
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【Leetcode】264. Ugly Number II ,丑数
原题 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime facto ...
- 【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 ...
随机推荐
- 【HANA系列】SAP ECLIPSE中创建ABAP项目的步骤
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP ECLIPSE中创建AB ...
- python基础--面向对象初始
# 类与对象,类是类别.种类,是面向对象设计中最重要的概念, # 对象是特征与技能的结合体, # 类是一系列对象相似特征与技能的结合体 # 例如:人是一个类,而我本人是一个对象,手,脚,是我的特征, ...
- chrome插件--安装以及问题记录
vue-devtools 插件网址下载 问题1 Vue.js is detected on this page. Devtools inspection is not available becaus ...
- 使用注解方式搭建SpringMVC
1.以前搭建Spring MVC 框架一般都使用配置文件的方式进行,相对比较繁琐.spring 提供了使用注解方式搭建Spring MVC 框架的方式,方便简洁.使用Spring IOC 作为根容器管 ...
- mysql8无法用navicat连接(mysql8加密方式的坑)
关键词:mysql8无法用navicat连接,navicat无法连接mysql8,mysql8,mysql8的加密方式 [1]mysql8 的坑 密码加密规则 在MySQL 8.0.以上版本中,cac ...
- linux-查询某软件的安装的目录
eg:jenkins\\\ rpm -ql jenkins 安装目录/var/lib/jenkins 配置文件 /etc/sysconfig/jenkins 日志目录 /var/log/jenkins ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- [LeetCode] 完全二叉树的节点个数
题目链接: https://leetcode-cn.com/problems/count-complete-tree-nodes 难度:中等 通过率:57.4% 题目描述: 给出一个 完全二叉树 ,求 ...
- 会引起全表扫描的几种SQL 以及sql优化 (转)
出处: 查询语句的时候尽量避免全表扫描,使用全扫描,索引扫描!会引起全表扫描的几种SQL如下 1.模糊查询效率很低: 原因:like本身效率就比较低,应该尽量避免查询条件使用like:对于like ‘ ...
- Codeforces1256F_Equalizing Two Strings
题意 给定两个字符串,可以任意选择s串的一段和t串的相同长度的一段进行翻转,无限次数,问能否通过翻转使得两个字符串相等. 分析 看了题解发现思路很巧妙. 无限次数的子串翻转其实就是相邻两个字符的交换. ...