[leetcode]483. Smallest Good Base
来源:互联网 发布:java传递参数基本类型 编辑:程序博客网 时间:2024/06/05 11:06
题目链接:https://leetcode.com/problems/smallest-good-base/description/
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1.
Now given a string representing n, you should return the smallest good base of n in string format.
Example 1:
Input: "13"Output: "3"Explanation: 13 base 3 is 111.
Example 2:
Input: "4681"Output: "8"Explanation: 4681 base 8 is 11111.
Example 3:
Input: "1000000000000000000"Output: "999999999999999999"Explanation: 1000000000000000000 base 999999999999999999 is 11.
Note:
- The range of n is [3, 10^18].
- The string representing n is always valid and will not have leading zeros.
class Solution {public: string smallestGoodBase(string n) { unsigned long long tn=(unsigned long long)stoll(n); unsigned long long x=1; for(int i=62;i>=1;i--) { if((x<<i)<tn) { unsigned long long cur=mysolve(tn,i); if(cur!=0) return to_string(cur); } } return to_string(tn-1); }private: unsigned long long mysolve(unsigned long long n,int d) { unsigned long long right=(unsigned long long)(pow(n,1.0/d)+1); unsigned long long left=1; while(left<=right) { unsigned long long mid=left+(right-left)/2; unsigned long long sum=1,cur=1; for(int i=1;i<=d;i++) { cur*=mid; sum+=cur; } if(sum==n) return mid; if(sum>n) right=mid-1; else left=mid+1; } return 0; }};
阅读全文
0 0
- leetCode 483. Smallest Good Base
- [leetcode]483. Smallest Good Base
- leetcode题解-483. Smallest Good Base
- [Leetcode] 483. Smallest Good Base 解题报告
- leetcode 483. Smallest Good Base 二分查找
- 483. Smallest Good Base
- 483. Smallest Good Base
- 483. Smallest Good Base
- 483. Smallest Good Base
- 483. Smallest Good Base
- LeetCode 483 Smallest Good Base 解题报告
- LeetCode 483 Smallest Good Base 题解
- Smallest Good Base
- Leetcode 483 - Smallest Good Base(二分+枚举)
- leetcode483. Smallest Good Base 的一些思考
- good blog about android base
- leetcode 632. Smallest Range
- [LeetCode]632. Smallest Range
- 技术文章 | 前端优化:9 个技巧_提高 Web 性能
- MaxCompute UDF系列之如何判断字符串中是否包含汉字
- Java面向对象
- mongo的upsert(spring的mongo template)
- sql多表查询
- [leetcode]483. Smallest Good Base
- 宏、函数、宏函数的区别
- Mysql中文模糊查询问题
- JS多物体透明度运动
- 经典WebView加载HTML布局示例
- Android 的网络请求需要开辟子线程的原因 +程序实例
- 括号配对问题
- 王学岗ToolBar,DrawLayout+Navigation的使用(二)
- 算法训练 字符串逆序