LeetCode #754 Reach a Number 到达终点数字

754 Reach a Number 到达终点数字

Description:
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:

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

题目描述:

在一根无限长的数轴上,你站在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]范围中的非零整数。

思路:

首先 target的正负性不影响结果, 比如 target = 3
3 = 1 + 2; -3 = -1 + (-2)
只要把每一步取相反数即可, 所以直接可以令 target = abs(target)
令 sum = 1 + 2 + 3 + ...
当 sum < target时, 要一直往右走, 直到 sum > target, 不然一定到不了 target
比如 target = 4. sum = 1 + 2 + 3 = 6, 6 - 4 = 2
这个时候如果sum - target是偶数, 那么只要把 sum中的一项替换成负数即可
比如 target = -1 + 2 + 3 = 4
如果这个时候sum - target是奇数, 那么还需要看 sum的最后一项

如果最后一项是奇数, 要多走两步
比如 target = 5, sum = 1 + 2 + 3 = 6, 6 - 5 = 1
target = 1 + 2 + 3 + 4 - 5 = 5
如果最后一项是偶数, 只要多走一步
比如 target = 9, sum = 1 + 2 + 3 + 4 = 10, 10 - 9 = 1
target = 1 + 2 - 3 + 4 + 5 = 9

sum = (1 + 2 + 3 + ... + i) = (1 + i) * i / 2
时间复杂度O(1), 空间复杂度O(1)

代码:
C++:

class Solution 
{
public:
    int reachNumber(int target) 
    {
        int s = 0, i = 1;
        for (target = abs(target); s < target or (s - target) % 2; i++) s += i;
        return --i;
    }
};

Java:

class Solution {
    public int reachNumber(int target) {
        int s = 0, i = 1;
        target = Math.abs(target);
        while (s < target || (s - target) % 2 != 0) s += i++;
        return --i;
    }
}

Python:

class Solution:
    def reachNumber(self, target: int) -> int:
        s, i, target = 0, 1, abs(target)
        while s < target or (s - target) % 2:
            s += i
            i += 1
        return i - 1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 7.骑车这次云南之行,最开心的事情之一就是骑车。在昆明,骑了一上午的电动车,去捞鱼河公园赏景;在大理,骑了一下午的...
    可回阅读 175评论 1 1
  • 1.情绪来了,提醒自己接纳情绪,带着一颗谦卑臣服的心。 2.怎样做到谦卑臣服呢? (1)清理文:对不起,请原谅,谢...
    Susan罗阅读 1,177评论 0 0
  • 周末,一边收拾屋子一边听书《不抱怨的力量》,书中的观点是”如果停止在日常生活中的抱怨,能让任何人走向成功。“对我的...
    兰汐兰阅读 581评论 0 1
  • 2012年,我正式成为了一名小学教师,幸运又幸福。记得小时候,就喜欢在大院里教小弟弟小妹妹们读书识字,那种教与学的...
    花朵quan阅读 107评论 0 0
  • 不知道从什么时候开始,喜欢上了调色,不同的颜料,两两相配或者三个乱抹就能调绘出各种不同的色彩,一条条,一道道的用笔...
    耗子先森阅读 252评论 1 2