## 题目描述：

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

## 打表Python代码：

class SolutionBFS(object):
def reachNumber(self, target):
"""
:type target: int
:rtype: int
"""
dp = {}
queue = [0]
step = 0
while queue:
step += 1
queue0 = set()
for loc in queue:
for delta in (-step, step):
nloc = loc + delta
dp[nloc] = min(step, dp.get(nloc, 0x7FFFFFFF))
if nloc == target:
return dp[nloc]
print list(queue0)
queue = queue0

print SolutionBFS().reachNumber(40)

[1, -1]
[1, 3, -3, -1]
[0, 2, 4, 6, -6, -4, -2]
[0, 2, 4, 6, 8, 10, -10, -8, -6, -4, -2]
[1, 3, 5, 7, 9, 11, 13, 15, -15, -13, -11, -9, -7, -5, -3, -1]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, -21, -19, -17, -15, -13, -11, -9, -7, -5, -3, -1]

1. 负数位置与正数位置对称
2. 当(N - 1) % 4 <= 1时，第N步（N从1开始）可以到达的位置为奇数，否则为偶数
3. 第N步可以到达的最大绝对值位置为：首项为1，公差为1的等差数列前N项和 S(n) = n * (n + 1) / 2
4. 令X为target，解方程S(n) = X得：n = (sqrt(1 + 8*x) - 1) / 2

## Python代码：

class Solution(object):
def reachNumber(self, target):
"""
:type target: int
:rtype: int
"""
sumn = lambda x: (1 + x) * x / 2
target = abs(target)
ans = int((math.sqrt( 1 + 8 * target) - 1) / 2)
maxn = sumn(ans)
if maxn < target:
ans += 1
maxn = sumn(ans)
if target % 2 != maxn % 2:
ans += 1 + ans % 2
return ans

Pingbacks已关闭。