到达一个数 Reach a Number
2018-09-24 14:19:58
问题描述:

问题求解:
初看到这个问题,直觉上认为可以通过BFS遍历解空间进行求解,因为本质上来说,这个问题和棋盘上移动马的问题是一类问题,都是可以转化成图的问题,但是MLE了,问题出在在本问题中是不能使用used来保存已经扩展过的节点的,因为相同的节点在不同的阶段的移动步数是不一样的,因此都需要进行入队列的操作。
当然,看到数据规模就应该有意识,这个问题是不能使用暴力搜索来求解的。
事实上,这个问题是一个数学问题,求解方案是:
1)首先负数和其相反数的步数是相同的,因此只需要考虑正数的个数;
2)对于一个正数,我们最先需要做的就是通过最短的步骤到达或将将超过这个target;
3)如果正好达到target,或者diff为一个偶数,那么我们可以直接返回step,因为如果diff为偶数,可以将前面的+改变成-实现和为target;
4)问题就是如果diff为奇数,那么就需要继续往后加,直到diff为偶数
public int reachNumber(int target) {
target = Math.abs(target);
int sum = 0;
int step = 0;
while (sum < target) {
step++;
sum += step;
}
while ((sum - target) % 2 != 0) {
step++;
sum += step;
}
return step;
}
到达一个数 Reach a Number的更多相关文章
- 领扣-754 到达终点数字 Reach a Number MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- [Swift]LeetCode754. 到达终点数字 | Reach a Number
You are standing at position 0 on an infinite number line. There is a goal at position target. On ea ...
- LeetCode 754. Reach a Number
754. Reach a Number(到达终点数字) 链接:https://leetcode-cn.com/problems/reach-a-number/ 题目: 在一根无限长的数轴上,你站在0的 ...
- 【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到达终点数字
题目 在一根无限长的数轴上,你站在0的位置.终点在target的位置. 每次你可以选择向左或向右移动.第 n 次移动(从 1 开始),可以走 n 步. 返回到达终点需要的最小移动次数. 示例 1: 输 ...
- [LeetCode] 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 ...
- Python3解leetcode Reach a Number
问题描述: You are standing at position 0 on an infinite number line. There is a goal at position target. ...
- 【LeetCode】754. Reach a Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 日期 题目地址:https://leetcod ...
随机推荐
- MySQL更改relay-bin名称导致同步停止的解决办法
今天在优化io的时候,移动了从库relay-bin的位置,并将hostname部分去掉了,启动后,从库slave状态如下: mysql> show slave status\G; ******* ...
- bzoj 2753 [SCOI 2012] 滑雪与时间胶囊 - Prim
题目传送门 传送点I 传送点II 题目大意 给定一个有$n$个点$m$条边的图,每个点有一个高度$h_{i}$,能从$u$经过一条边到达$v$,当且仅当存在一条边是$(u, v)$或$(v, u)$, ...
- 盒子总结,文本属性操作,reset操作,高级选择器,高级选择器优先级,边界圆角(了解),a标签的四大伪类,背景图片操作,背景图片之精灵图
盒子总结 ''' block: 设置宽高 1.没有设置宽,宽自适应父级的宽(子级的border+padding+width=父级的width) 2.没有设置高,高由内容撑开 设置了宽高 一定采用设置的 ...
- C#操作字符串方法总结
/* ######### ############ ############# ## ########### ### ###### ##### ### ####### #### ### ####### ...
- IDEA查看一个类的所有继承关系
通常一个.java文件对应一个java类. 鼠标右击一个类: 即可查看.按住alt键可放大. 另一快捷键:光标在类名上,ctrl+H
- js二叉树
插入数值//初始化node对象function Node ( data) { this.data = data; this.left = null; this.right = null;}// 定义插 ...
- 题解—— 洛谷 p1993 小K的农场(差分约束&负环判断)
看到题就可以想到差分约束 判断负环要用dfs,bfs-spfa会TLE 4个点 bfs-spfa #include <cstdio> #include <algorithm> ...
- 论文笔记:Tracking by Natural Language Specification
Tracking by Natural Language Specification 2018-04-27 15:16:13 Paper: http://openaccess.thecvf.com/ ...
- 论文阅读: End-to-end Learning of Action Detection from Frame Glimpses in Videos
End-to-End Learning of Action Detection from Frame Glimpses in Videos CVPR 2016 Motivation: 本 ...
- Codeforces 855C. Helga Hufflepuff's Cup----树形DP
z最近在学习树形DP...好难啊. 在cf上找到了一题c题当模版马克一下. 题目不贴了..>>http://codeforces.com/problemset/problem/855/C& ...