-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
397.cpp
37 lines (36 loc) · 1.02 KB
/
397.cpp
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
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public:
int integerReplacement(int n) {
int count = 0;
if(n==INT_MAX)return 32;
while(n != 1){
if(n % 2 == 0) n = n / 2;
else if((n / 2) % 2 == 0 || n / 2 == 1)
n = n - 1;
else
n = n + 1;
count++;
}
return count;
}
};
__________________________________________________________________________________________________
sample 8304 kb submission
class Solution {
public:
int integerReplacement(long n) {
int count = 0;
while(n != 1){
if(n % 2 == 0) n = n / 2;
else if((n / 2) % 2 == 0 || n / 2 == 1)
n = n - 1;
else
n = n + 1;
count++;
}
return count;
}
};
__________________________________________________________________________________________________