Class Solution

java.lang.Object
g0701_0800.s0754_reach_a_number.Solution

public class Solution extends Object
754 - Reach a Number.<p>Medium</p> <p>You are standing at position <code>0</code> on an infinite number line. There is a destination at position <code>target</code>.</p> <p>You can make some number of moves <code>numMoves</code> so that:</p> <ul> <li>On each move, you can either go left or right.</li> <li>During the <code>i<sup>th</sup></code> move (starting from <code>i == 1</code> to <code>i == numMoves</code>), you take <code>i</code> steps in the chosen direction.</li> </ul> <p>Given the integer <code>target</code>, return <em>the <strong>minimum</strong> number of moves required (i.e., the minimum</em> <code>numMoves</code><em>) to reach the destination</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> target = 2</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong></p> <p>On the 1<sup>st</sup> move, we step from 0 to 1 (1 step).</p> <p>On the 2<sup>nd</sup> move, we step from 1 to -1 (2 steps).</p> <p>On the 3<sup>rd</sup> move, we step from -1 to 2 (3 steps).</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> target = 3</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>On the 1<sup>st</sup> move, we step from 0 to 1 (1 step).</p> <p>On the 2<sup>nd</sup> move, we step from 1 to 3 (2 steps).</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li> <li><code>target != 0</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • reachNumber

      public int reachNumber(int target)