397. 整数替换

时间:2020-04-28 18:51:45   收藏:0   阅读:46
 1 class Solution 
 2 {
 3 public:
 4     int integerReplacement(int n) 
 5     {
 6         return DFS(n);
 7     }
 8 
 9     long long DFS(long long n)
10     {
11         if(n == 1) return 0;
12         if(n & 1) return min(DFS(n + 1),DFS(n - 1)) + 1;
13         else return DFS(n/2) + 1;
14     }
15 };

 

原文:https://www.cnblogs.com/yuhong1103/p/12796256.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!