leetCode 483. Smallest Good Base
来源:互联网 发布:网络订票如何选下铺 编辑:程序博客网 时间:2024/06/04 19:45
1.题目
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.
The range of n is [3, 10^18].
Example 1:
Input: “13”
Output: “3”
Explanation: 13 base 3 is 111.
2.算法
这个题的意思是找到最小基数,那么所得到的全为1的个数肯定最长,我们先找一个比较长的全为1的数,来找一下基数,如果不行则去一个1,在找基数,直到找到为止。m 的取值范围是 [2, log(n, 2)]当m取定时,k的取值范围是
public String smallestGoodBase(String n){long i = Long.parseLong(n);int maxLong = (int)(Math.log(i) / Math.log(2) + 1);for (int j = maxLong; j >= 2; j--){int k = find(i, j);if (k != 0) return k + "";}return i - 1 + "";}public int find(long i, int l){int ll = 2;int lr = (int)Math.pow(i, 1.0/l);while (ll <= lr){int mid = ll + (lr - ll) / 2;long sum = 0; int t = 1;for (int j = 0; j <= l; j++){sum += t;t *= mid;}if (sum == i) return mid;else if (sum > i) lr = mid - 1;else ll = 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
- 关于JFrame里setbackground()失效的解释
- CodeForces
- 超越spark性能数百倍的性能测试
- 8 HBase结构介绍
- PackageManagerService分析之一
- leetCode 483. Smallest Good Base
- 7 MapReduce进阶之shuffle阶段
- SpringMVC概念原理分析
- 查看内存使用情况
- 关于java代码jtable不显示表头的解决
- 互联网+农业:十万亿市场的各个环节都在这里了
- Servlet监听器
- Trivial Problem CodeForces
- JavaScript for Kids 学习笔记7. 函数