397. 整数替换

 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 };

 

posted @ 2020-04-28 18:24  Jinxiaobo0509  阅读(80)  评论(0)    收藏  举报