-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 090 - Reach a Number
45 lines (36 loc) · 1 KB
/
Day 090 - Reach a Number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// ( --- https://leetcode.com/problems/reach-a-number/ ---)
class Solution {
public:
int reachNumber(int target) {
// int pointer = 0;
// int step = 0;
// while(pointer != target){
// // int step = 0;
// if(pointer + step +1 < target){
// step++;
// pointer = pointer+step;
// }
// if(pointer + step +1 > target){
// step++;
// pointer = pointer-step;
// }
// if(pointer + step + 1 == target){
// step++;
// pointer = pointer+step;
// }
// }
// return step;
int sum = 0;
int steps = 0;
target = abs(target);
while(sum < target){
sum += steps;
steps++;
}
while((sum-target)%2 == 1){
sum += steps;
steps++;
}
return steps-1;
}
};