LeetCode 397. Integer Replacement

    xiaoxiao2022-06-30  41

    描述

    问一个数经过多少次的变换才会变成1

    解决

    利用递归


    class Solution { public: int integerReplacement(int n) { if (n == INT_MAX){ return 32; } if (n == 1){ return 0; } if (!(n & 1)){ return 1 + integerReplacement(n / 2); }else{ return min(1 + integerReplacement(n + 1), 1 + integerReplacement(n - 1)); } } };
    转载请注明原文地址: https://ju.6miu.com/read-1125613.html

    最新回复(0)