We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9cd8759 commit a1e2a05Copy full SHA for a1e2a05
Reach a Number/Reach_a_Number.cpp
@@ -0,0 +1,31 @@
1
+// 附参考链接
2
+// https://leetcode.com/problems/reach-a-number/discuss/112968/Short-JAVA-Solution-with-Explanation
3
+
4
+class Solution
5
+{
6
+public:
7
+ int reachNumber(int target)
8
+ {
9
+ target = target > 0 ? target : -target;
10
11
+ int pos = 0;
12
+ int step = 1;
13
14
+ while (pos + step < target)
15
16
+ pos += step;
17
+ ++step;
18
+ }
19
20
+ if (pos + step == target) return step;
21
22
+ int dis = step - (target - pos);
23
24
+ if ((dis & 1) == 0)
25
+ return step;
26
+ else if((step + 1) & 1)
27
+ return step + 1;
28
+ else
29
+ return step + 2;
30
31
+};
Reach a Number/Reach_a_Number.py
@@ -0,0 +1,3 @@
+class Solution:
+ def reachNumber(self, target: int) -> int:
0 commit comments