到达一个数 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;
    }

原文地址:https://www.cnblogs.com/TIMHY/p/9695448.html

时间: 2024-08-30 17:47:00

到达一个数 Reach a Number的相关文章

[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 rea

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 rea

键盘输入一个数number,输出从1到number的所有奇数和偶数

最近看到一个题,键盘输入一个数number,输出从1到number的所有奇数和偶数,有好几种算法 一. 取余+集合算法输出: 1 package com.company; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Scanner; 8 9 public class Test3 { 10

Majority Number I &amp; || &amp;&amp; |||

Majority Number Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Example Given [1, 1, 1, 1, 2, 2, 2], return 1 分析: 既然这里只有一个majority number,那么它的个数减去其它number个数之和还是为正值. 1 public

Lucky Number

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=209995 题目大意是说,定义一个数的lucky number是距离i最远的j且满足(a[i]<a[j] i<j). 问对所有数最大的lucky number是什么. 不必线段树. 我们可以先处理出a[i]的最远点.倒着扫一遍即可. 然后可以处理出大于等于a[i]的最远位置. 1 /**********************************************

fzu 2109 Mountain Number 数位DP

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2109 题意: 如果一个>0的整数x,满足a[2*i+1] >= a[2*i]和a[2*i+2],则这个数为Mountain Number. 给出L, R,求区间[L, R]有多少个Mountain Number. 思路: 数位DP,判断当前是偶数位还是奇数位(从0开始),如果是偶数位,那么它要比前一个数的值小, 如果是奇数位,那么它要比前一个数的值大. 1 #include <iostream>

leetcode之Happy Number

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the

LeetCode 第 263 题 (Ugly Number)

LeetCode 第 263 题 (Ugly Number) Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another pr

LeetCode OJ 202. Happy Number

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the