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:

  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))。

代码:

 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的更多相关文章

  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. [C#学习笔记]你真的理解拆箱装箱吗?

    学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!没错我的前言就是这个. 装箱 首先来看下,下面这段代码 可以看到,每次循环迭代都会初 ...

  2. Java实现对ftp的读写文件

    这里仅仅是对ftp工具类的简单使用,很多东西还不是很了解.当然学以致用,先用到这里吧. public class FtpTest { /** * 向ftp写文件(数据) */ @Test public ...

  3. (转载)Oracle的悲观锁和乐观锁

    为了得到最大的性能,一般数据库都有并发机制,不过带来的问题就是数据访问的冲突.为了解决这个问题,大多数数据库用的方法就是数据的锁定. 数据的锁定分为两种方法,第一种叫做悲观锁,第二种叫做乐观锁.什么叫 ...

  4. java学习笔记—第三方数据库连接池包1(29)

    第一步:导入dbcp包 第二步:通过核心类连接数据 BasicDataSource它是javax.sql.DataSrouce的子类. 一个工具类:BasicDataSourceFactory. 手工 ...

  5. jQuery中animate与scrollTop、offset().top实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. “全栈2019”Java第一百零二章:哪些作用域可以声明局部内部类?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  7. 使用memcache或redis限制某个用户或者某ip用户一段时间内最大投票次数

    实现每个用户在某网站10分钟内最多投票5次 function isFrequently($key){ $t = 60*10; $n = 5; $mem = new Memcache(); $mem-& ...

  8. 基于.NET的开源搜索引擎-DotLucene(2)

    NLucene是将 Lucene 从 Java 移植到 .NET 的一个 SourceForge 项目,它从 Lucene 1.2 版本转化而来. Lucene.Net因为 NLucene 项目到20 ...

  9. jquery源码解析:jQuery数据缓存机制详解1

    jQuery中有三种添加数据的方法,$().attr(),$().prop(),$().data().但是前面两种是用来在元素上添加属性值的,只适合少量的数据,比如:title,class,name等 ...

  10. mxonline实战12, 课程评论,相关课程推荐,课程视频页

    对应github地址:第12天   一. 课程评论   1. 创建URL, VIEW courses/views.py -> Course