mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
41 lines
1.4 KiB
HTML
41 lines
1.4 KiB
HTML
|
<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> </p>
|
||
|
<p><strong>Example 1:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> target = 2
|
||
|
<strong>Output:</strong> 3
|
||
|
<strong>Explanation:</strong>
|
||
|
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).
|
||
|
</pre>
|
||
|
|
||
|
<p><strong>Example 2:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong> target = 3
|
||
|
<strong>Output:</strong> 2
|
||
|
<strong>Explanation:</strong>
|
||
|
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).
|
||
|
</pre>
|
||
|
|
||
|
<p> </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>
|