[Leetcode] 754. 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].

    思路

    我开始用BFS实现了,但是TLE了。如果把移动n步所可以达到的位置记录下来,则我们可以得到如下的表格:

    [0]
    [-1, 1]
    [-3, 1, -1, 3]
    [-6, 0, -2, 4, -4, 2, 0, 6]
    [-10, -2, -4, 4, -6, 2, 0, 8, -8, 0, -2, 6, -4, 4, 2, 10]
    [-15, -5, -7, 3, -9, 1, -1, 9, -11, -1, -3, 7, -5, 5, 3, 13, -13, -3, -5, 5, -7, 3, 1, 11, -9, 1, -1, 9, -3, 7, 5, 15]
    [-21, -9, -11, 1, -13, -1, -3, 9, -15, -3, -5, 7, -7, 5, 3, 15, -17, -5, -7, 5, -9, 3, 1, 13, -11, 1, -1, 11, -3, 9, 7, 19, -19, -7, -9, 3, -11, 1, -1, 11, -13, -1, -3, 9, -5, 7, 5, 17, -15, -3, -5, 7, -7, 5, 3, 15, -9, 3, 1, 13, -1, 11, 9, 21]
    [-28, -14, -16, -2, -18, -4, -6, 8, -20, -6, -8, 6, -10, 4, 2, 16, -22, -8, -10, 4, -12, 2, 0, 14, -14, 0, -2, 12, -4, 10, 8, 22, -24, -10, -12, 2, -14, 0, -2, 12, -16, -2, -4, 10, -6, 8, 6, 20, -18, -4, -6, 8, -8, 6, 4, 18, -10, 4, 2, 16, 0, 14, 12, 26, -26, -12, -14, 0, -16, -2, -4, 10, -18, -4, -6, 8, -8, 6, 4, 18, -20, -6, -8, 6, -10, 4, 2, 16, -12, 2, 0, 14, -2, 12, 10, 24, -22, -8, -10, 4, -12, 2, 0, 14, -14, 0, -2, 12, -4, 10, 8, 22, -16, -2, -4, 10, -6, 8, 6, 20, -8, 6, 4, 18, 2, 16, 14, 28]
    这个还不够清楚。如果我们将每行都进行排序,则可以得到如下结果:

    [0]
    [-1, 1]
    [-3, -1, 1, 3]
    [-6, -4, -2, 0, 2, 4, 6]
    [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]
    [-15, -13, -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11, 13, 15]
    [-21, -19, -17, -15, -13, -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
    [-28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
    [-36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36]
    [-45, -43, -41, -39, -37, -35, -33, -31, -29, -27, -25, -23, -21, -19, -17, -15, -13, -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45]
    发现什么规律了吗?总结起来就是:

    1)在每一行中,如果target在该行中,则-target也一定在该行中。

    2)每一行的中的所有数的奇偶性相同。

    3)第n行的最大值(最小值)就是连续向右(左)走所可以达到的位置,最大值就是n * (n + 1) / 2,最小值就是-n * (n + 1) / 2。

    4)如果target介于某一行的最大值和最小值之间,其奇偶性和该行数的奇偶性相同,那么target一定在这一行中。

    有了以上观察,则思路就清楚了:我们一直累加sum直到sum >= target并且他们两个的奇偶性相同。那么此时target一定在step所在的行,所以返回step就可以了。

    代码

    class Solution {
    public:
        int reachNumber(int target) {
            target = abs(target);
            int step = 0, sum = 0;
            while (sum < target || (sum - target) % 2 != 0) {
                sum += (++step);
            }
            return step;
        }
    };
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值