LeetCode 754. Reach a Number (Java版; Medium)

welcome to my blog

LeetCode 754. Reach a Number (Java版; Medium)

题目描述
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.

第一次做; 参考了题解的思想 核心: 1)假设target为正, 我们只往右走并且记录走过的长度,用sum表示, 如果sum>target, 说明需要需要往左走, 但是什么时候才能往左走呢? 看下面的示例
假设target=10
step=5时, 此时有sum=1+2+3+4+5=15, 如果让step=3那一步往左走, sum减少了两个3, 也就是15-2*3=9,
其实任何一步s往左走时, 都会使得sum的值减少2*s, 所以如果有一个时刻,sum-target是个偶数, 
用a表示 说明可以让之前的第a步往左走!

step=7时, 此时有sum=1+2+3+4+5+6+7=28, 此时sum-target=18, 18/2=9, 可以让第4步和第5步往左走, 或者让第3步和第6步往左走 
class Solution {
    public int reachNumber(int target) {
        if(target==0){
            return 0;
        }
        int step=1;
        int sum=0;
        target = Math.abs(target);
        while(true){
            sum += step;
            if(sum==target){
                return step;
            }
            else if(sum<target){
                step++;
                continue;
            }
            else{
                if((sum-target)%2==0){
                    return step;
                }else{
                    step++;
                    continue;
                }
            }
        }
    }
}
第一次做; 超时:31/71, 非常笨重的方法; 计算最多走maxN步时能到达的位置, 看看target属不属于其中之一, 属于的话返回maxN, 不属于的话增加maxN重新探索
//暴力递归
class Solution {
    public int reachNumber(int target) {
        int n = 1;
        while(true){
            HashSet<Integer> set = new HashSet<>();
            core(0, 1, n, set);
            if(set.contains(target)){
                return n;
            }
            n++;
        }
        // return -1;
    }

    /*
    递归函数逻辑: 当前是第n步, 当前处于位置cur, 目标为target时, 记录向左走需要的移动次数和向右走需要的移动次数, 返回值更小的次数
    当前条件和新条件新递归融在一块了
    */
    private void core(int cur, int n, int maxN, HashSet<Integer> set){
        if(n>maxN){
            return;
        }
        set.add(cur-n);
        set.add(cur+n);
        core(cur-n, n+1, maxN, set);
        core(cur+n, n+1, maxN, set);
    }
}
LeetCode优秀题解
First try to see if we can get answer by just summing up values. If we undercount just add the next step.

If we overcount then we have to look at the difference between the sum and target.

The reason why we can't have odd values as the difference is because if we swap a value from negative to
positive we don't just lose the value. We lose double the value. Right?

If our sum is:
1 + 2 + 3 + 4 + 5 = 15

Say we flip 3 -> -3. We don't just have sum = 12 now. We have sum = 9.

1 + 2 -3 + 4 + 5 = 9 = 15 - 2(3).

So whenever you subtract any value from the sum you really lose double the value. And we know all even
numbers are double some other number. Therefore our difference between our target and sum has to be even.
class Solution {
    public int reachNumber(int target) {
        target = Math.abs(target);
        int step = 0;
        int sum = 0;
        while (sum < target) {
            step++;
            sum += step;
        }
        while ((sum - target) % 2 != 0) {
            step++;
            sum += step;
        }
        return step;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值