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:

  1. Input: ""
  2. Output: ""
  3. Explanation: base is .

Example 2:

  1. Input: ""
  2. Output: ""
  3. Explanation: base is .

Example 3:

  1. Input: ""
  2. Output: ""
  3. Explanation: base is .

Note:

  1. The range of n is [3, 10^18].
  2. 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))。

代码:

  1. public class Solution {
  2. public String smallestGoodBase(String n) {
  3. long num = Long.parseLong(n);
  4. int maxIndex = (int) (Math.log(num)/Math.log(2) + 1);
  5. for(int i = maxIndex; i >= 3; i--) {
  6. long base = (long)Math.pow(num, 1.0 / (i - 1)), sum = 1, cur = 1;
  7. for(int j = 1; j < i; j++) {
  8. sum += (cur *= base);
  9. }
  10. if(sum == num) return String.valueOf(base);
  11. // java中没有无符号数,而且Math.pow(base, i) - 1存在精度问题,例如样例"14919921443713777",结果应该为"496",但是下列语句的结果是"14919921443713776"
  12. // if((long)((Math.pow(base, i) - 1) / (base - 1)) == num) return String.valueOf(base);
  13. }
  14. return String.valueOf(num - 1);
  15. }
  16. }

Leetcode 483. Smallest Good Base的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. ASP.NET:邮件服务器与客户端

    目录: 一.概述 二.MX设置 三.使用系统的SMTP功能发邮件 四.使用hMailServer收发邮件 五.Web邮件客户端 一.概述 首先必须清楚SMTP才是提供邮件服务器的核心,收发邮件全靠SM ...

  2. gearman服务连接php java

    在实际工作中,会碰到两个问题 (1)现有系统想集成一个开发组件,而该组件的SDK却没有现有语言版本. (2)系统中的一项功能非常耗费资源,最好能利用其它机器来处理. 本文介绍gearman的使用,实现 ...

  3. Centos7下修复 视频播放器(先 安装VLC视频播放器)

    用最新的CentOS7发现没有视频播放器,于是在http://pkgs.org/上查找,发现了nux dextop仓库上有, 于是到他的官网上http://li.nux.ro/repos.html查了 ...

  4. Tomcat不自动解压问题

    问题: 版本迭代上线,需要更换新的war包, 1.先将老的war和文件夹删除掉,再放入新的war到webapps中, 2.发现启动Tomcat后没有解压该war包, 3.需要先将server.xml中 ...

  5. ce+od无法同时附加进程的问题

    CE+OD无法附加游戏进程的破解方法 来吧 别在为这烦恼了 其实看过 windows 核心编程那本书的人都知道 计算机编程领域 那些所谓的游戏保护 真的只是为难菜鸟而已,对于大鸟基本不起作用. 游戏无 ...

  6. JAVA 字符串编码转换

    /** * 字符串编码转换的实现方法 * @param str 待转换编码的字符串 * @param newCharset 目标编码 * @return * @throws UnsupportedEn ...

  7. DevExpress GridControl使用教程:之 添加 checkbox 复选框

    添加一列,FieldName为"FLAG",将ColumnEdit设置为复选框样式. gridview1   =>optionsbehavior =>  editabl ...

  8. WP8.1StoreApp(WP8.1RT)---SystemTray的变化

    原Microsoft.Phone.Shell中的SystemTray,已经改到Windows.UI.ViewManagement中StatusBar了. 只能在代码中设置相关属性. 如: 1 2 3 ...

  9. LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)

    http://lightoj.com/volume_showproblem.php?problem=1213  Fantasy of a Summation Time Limit:2000MS     ...

  10. kolla-ansible 重新部署 ceph-mon 组件

    1.备份数据 [root@controller ~]# mv /var/lib/docker/volumes/ceph_mon /var/lib/docker/volumes/ceph_backup/ ...