Class Solution
-
- All Implemented Interfaces:
public final class Solution
754 - Reach a Number\.
Medium
You are standing at position
0
on an infinite number line. There is a destination at positiontarget
.You can make some number of moves
numMoves
so that:On each move, you can either go left or right.
During the <code>i<sup>th</sup></code> move (starting from
i == 1
toi == numMoves
), you takei
steps in the chosen direction.
Given the integer
target
, return the minimum number of moves required (i.e., the minimumnumMoves
) to reach the destination.Example 1:
Input: target = 2
Output: 3
Explanation:
On the 1<sup>st</sup> move, we step from 0 to 1 (1 step).
On the 2<sup>nd</sup> move, we step from 1 to -1 (2 steps).
On the 3<sup>rd</sup> move, we step from -1 to 2 (3 steps).
Example 2:
Input: target = 3
Output: 2
Explanation:
On the 1<sup>st</sup> move, we step from 0 to 1 (1 step).
On the 2<sup>nd</sup> move, we step from 1 to 3 (2 steps).
Constraints:
<code>-10<sup>9</sup><= target <= 10<sup>9</sup></code>
target != 0
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final Integer
reachNumber(Integer target)
-
-
Method Detail
-
reachNumber
final Integer reachNumber(Integer target)
-
-
-
-