File tree Expand file tree Collapse file tree 1 file changed +16
-30
lines changed Expand file tree Collapse file tree 1 file changed +16
-30
lines changed Original file line number Diff line number Diff line change @@ -15,23 +15,16 @@ class Solution {
15
15
16
16
private:
17
17
int power_value (int x) {
18
- int y = x;
19
- if (!Solution::dp_.count (y)) {
20
- int result = 0 ;
21
- while (x > 1 ) {
22
- ++result;
23
- if (x & 1 ) {
24
- x = 3 * x + 1 ;
25
- } else {
26
- x >>= 1 ;
27
- }
28
- if (Solution::dp_.count (x)) {
29
- result += Solution::dp_[x];
30
- break ;
31
- }
18
+ int y = x, result = 0 ;
19
+ while (x > 1 && !Solution::dp_.count (x)) {
20
+ ++result;
21
+ if (x & 1 ) {
22
+ x = 3 * x + 1 ;
23
+ } else {
24
+ x >>= 1 ;
32
25
}
33
- Solution::dp_[y] = result;
34
26
}
27
+ Solution::dp_[y] = result + Solution::dp_[x];
35
28
return Solution::dp_[y];
36
29
}
37
30
@@ -55,23 +48,16 @@ class Solution2 {
55
48
56
49
private:
57
50
int power_value (int x) {
58
- int y = x;
59
- if (!Solution2::dp_.count (y)) {
60
- int result = 0 ;
61
- while (x > 1 ) {
62
- ++result;
63
- if (x & 1 ) {
64
- x = 3 * x + 1 ;
65
- } else {
66
- x >>= 1 ;
67
- }
68
- if (Solution2::dp_.count (x)) {
69
- result += Solution2::dp_[x];
70
- break ;
71
- }
51
+ int y = x, result = 0 ;
52
+ while (x > 1 && !Solution2::dp_.count (x)) {
53
+ ++result;
54
+ if (x & 1 ) {
55
+ x = 3 * x + 1 ;
56
+ } else {
57
+ x >>= 1 ;
72
58
}
73
- Solution2::dp_[y] = result;
74
59
}
60
+ Solution2::dp_[y] = result + Solution2::dp_[x];
75
61
return Solution2::dp_[y];
76
62
}
77
63
You can’t perform that action at this time.
0 commit comments