Leetcode 483. Smallest Good Base
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: ""
Output: ""
Explanation: base is .
Example 2:
Input: ""
Output: ""
Explanation: base is .
Example 3:
Input: ""
Output: ""
Explanation: base is .
Note:
- The range of n is [3, 10^18].
- The string representing n is always valid and will not have leading zeros.
Next challenges: Palindrome Number Best Meeting Point Encode and Decode TinyURL
思路:首先完成字符串到数字的转换,然后对于特定的num,当前的最长的其他进制的表示长度是进制为2时的表示长度,就是log2(num)+1(注意:10..0有t个0,那么10..0=2^t)。那么指数i的遍历区间就是[1,log2(num)+1]。对于当前数的当前指数i,可能的base整数取值是num^(1/(i-1))。
代码:
public class Solution {
public String smallestGoodBase(String n) {
long num = Long.parseLong(n);
int maxIndex = (int) (Math.log(num)/Math.log(2) + 1);
for(int i = maxIndex; i >= 3; i--) {
long base = (long)Math.pow(num, 1.0 / (i - 1)), sum = 1, cur = 1;
for(int j = 1; j < i; j++) {
sum += (cur *= base);
}
if(sum == num) return String.valueOf(base);
// java中没有无符号数,而且Math.pow(base, i) - 1存在精度问题,例如样例"14919921443713777",结果应该为"496",但是下列语句的结果是"14919921443713776"
// if((long)((Math.pow(base, i) - 1) / (base - 1)) == num) return String.valueOf(base);
}
return String.valueOf(num - 1);
}
}
Leetcode 483. Smallest Good Base的更多相关文章
- [LeetCode] 483. Smallest Good Base 最小的好基数
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 str ...
- 483. Smallest Good Base
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 str ...
- [LeetCode] Smallest Good Base 最小的好基数
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 str ...
- [Swift]LeetCode483. 最小好进制 | Smallest Good Base
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 str ...
- Binary Search-483. Smallest Good Base
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 str ...
- [LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- Leetcode: K-th Smallest in Lexicographical Order
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...
随机推荐
- ssl协议,openssl,创建私有CA
SSL是Security Socket Layer:安全的套接字层 他介于HTTP和TCP协议层之间 SSL是Netscape公司开发的,属于个人 TLS是标准委员会制定的 OpenSSL是SSL的开 ...
- JQuery中checkbox选择器
今天我们讲的是如何选择HTML网页中CheckBox选择器 如下图,是几个checkbox <input type='checkbox'checked="checked"/& ...
- js虚拟数字小键盘
效果图 页面代码: @Html.TextBoxFor(m=>Model.FBP[i].RealNumb,new{onclick="showKeyboard('txtRealNumbOp ...
- C# skip 重试执行代码段
var retryTimes = 5; //重试次数 int times = 0; skip: //代码段开始 //处理逻辑 var result=false ; // ...
- 使用NPOI时ICSharpCode.SharpZipLib版本冲突问题解决
系统原来引用的ICSharpCode.SharpZipLib是0.84版本的, 添加了2.3版本的NPOI引用后,报版本冲突错误,因为NPOI用的ICSharpCode.SharpZipLib是0.8 ...
- ES6——内置对象的扩展
字符串的扩展 //模版字符串 let flag=true; let hml=`<ul> <li> <span></span> <span>& ...
- MySQL多线程备份工具mydumper 之 RDS外部实例迁移平台
此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.Format_description_event问题: BINLOG ' kTXkUxMKAAAALQA ...
- django系列3.4-- request对象和response对象(未完待续)
一.request对象 详细信息可以查阅django官方文档 共有五种请求相关的常用值 request.path_info 返回用户访问的url不包括域名 request.method 请求中使用的H ...
- WebService-php- 2(17)
wsdl实例 <?xml version ='1.0' encoding ='UTF-8' ?> <definitions targetNamespace='http://local ...
- jquery中通过添加readonly或者disabled属性实现行为禁止 / 去除某个属性的方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...