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:

  1. The range of n is [3, 10^18].
  2. The string representing n is always valid and will not have leading zeros.

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 12 return String.valueOf(num - 1);
13 }
14 }

 

Binary Search-483. Smallest Good Base的更多相关文章

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

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

  4. Binary search tree system and method

    A binary search tree is provided for efficiently organizing values for a set of items, even when val ...

  5. Optimal binary search trees

    问题 该问题的实际应用 Suppose that we are designing a program to translate text from English to French. For ea ...

  6. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  7. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

随机推荐

  1. 使用RSA进行信息加密解密的WebService示例

    使用RSA进行信息加密解密的WebService示例 按:以下文字涉及RSA对WebService传递的数据的加密解密,如果您已经熟知RSA或是有其它更好的方法请不要往下看以免浪费时间. WebSer ...

  2. linux的零碎知识

    一  nfs服务器 1  NFS的介绍:是Network File System的简写,是网络文件系统.用于分散式文件系统的协定,由sun公司开发的,在1984年向外公布的. 2  NFS的功能:是通 ...

  3. newton法分形图

    方程:z^6-1=0; %f为求解的方程,df是导数,使用的时候用funchandler定义 %res是目标分辨率,iter是循环次数,(xc,yc)是图像的中心,xoom是放大倍数 %参数视自己需求 ...

  4. JedisPoolConfig解说

    版本一 今天发现Jedis 默认的连接方式 jedis=new Jedis(“localhost”,6379),老是发生connection timeout. 后来发现jedis类包还有一种可以设置最 ...

  5. win7-64bit下安装Scipy

    一直用MAC写python,但京东给的本装的是win7系统,在安装scipy时各种报错,最后错误提示为: no lapack/blas resources found 开始一顿搜,爆栈给出的解决方案是 ...

  6. 03-vue-router

    前端路由的实现原理 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  7. HDU 1716 排列2 (格式问题+排列)

    题意:. 析:我们完全可以STL里面的函数next_permutation(),然后方便,又简单,这个题坑就是在格式上. 行末不能有空格,结尾不能有空行,不大好控制,必须控制好第一次数. 这个题本应该 ...

  8. C#中验证sql语句是否正确(不执行语句)

    SET PARSEONLY检查每个 Transact-SQL 语句的语法并返回任何错误消息,但不编译和执行语句.SET PARSEONLY { ON | OFF }当 SET PARSEONLY 为 ...

  9. ubuntu16.4中开启vncserver进行远程桌面

    使用x11vnc作为vncserver端 1 安装x11vnc $ sudo apt-get update $ sudo apt-get install x11vnc 2 生成密码 $ x11vnc ...

  10. (求树的直径)Warm up -- HDU -- 4612

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 给一个无向图, 加上一条边后,求桥至少有几个: 那我们加的那条边的两个顶点u,v:一定是u,v之 ...