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. Golang之并发篇

    进程和线程 A.进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单位. B.线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位. C.一 ...

  2. 运行代码后出现Process finished with exit code 0是为什么?

    Process finished with exit code 0 意味着你的程序正常执行完毕并退出. 可以科普一下exit code,在大部分编程语言中都适用: exit code 0 表示程序执行 ...

  3. Speeding up Homestead on Windows Using NFS

    Speeding up Homestead on Windows Using NFS Sep 07 2015 Homestead Laravel EDIT: I have another articl ...

  4. Java数据结构和算法(一)线性结构

    Java数据结构和算法(一)线性结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 线性表 是一种逻辑结构,相同数据类型的 ...

  5. 通过命令更新git for windows

    git update-git-for-windows 参考: https://github.com/PanJiaChen/vue-element-admin/issues/24 https://sta ...

  6. UVa 10970 Big Chocolate (想一下就AC了)

    题意:给你一个m*n的巧克力,让人把它切成1*1的,但是每次只能切一下,问要切多少刀. 析:简单啊,我就不明白了 怎么那么多人TLE了,不会当DP做了吧,其实不用的. 假设有一个1*m的巧克力,很明显 ...

  7. 点云库PCL学习

    1. 点云的提取 点云的获取:RGBD获取 点云的获取:图像匹配获取(通过摄影测量提取点云数据) 点云的获取:三维激光扫描仪 2. PCL简介 PCL是Point Cloud Library的简称,是 ...

  8. _variant_t与其他数据类型的转换

    转自:http://kuaixingdong.blog.hexun.com/29627840_d.html 我们先看看COM所支持的一些类型的基本类: (微软提供,在comdef.h中定义) 在COM ...

  9. 菜鸟——springboot+mybatis+maven

    网上找了很多资料,学习如何搭建springboot,由于刚刚接触springboot,不是很熟练,通过参考网上别人搭建的例子,自己也搭建了一个简单的springboot+mybaits+maven 网 ...

  10. delphi存取图片

    1.存图片到数据库 var   PicStream: TMemoryStream; if imgBugPic.Picture.Graphic <> nil then  begin    P ...