Skip to content

Commit a1e2a05

Browse files
Add files via upload
1 parent 9cd8759 commit a1e2a05

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Reach a Number/Reach_a_Number.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -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

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def reachNumber(self, target: int) -> int:
3+

0 commit comments

Comments
 (0)