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. tomcat启动时就频繁gc和full gc

    一个小业务,流量并不大,功能也很简单,spring framework+mybatis+quartz,一启动就看到gc的频次和full gc的频次非常高: 4.202: [Full GC 4.202: ...

  2. Unity3d之Coroutine

    在Unity3d中使用C#时,Coroutine是一个大有用处的好东西,至于怎么用网上多的是讲,我仅在此记录最近一次使用中的小发现. 因为某种需求,要在一个Coroutine实现中使用while循环, ...

  3. 在DOS命令行窗口中显示系统环境环境变量

    (这是一个小技巧) 示例命令: echo %path% path是系统环境变量,使用百分号包围起来 http://www.cnblogs.com/danzhang 张洪君 微软ALM MVP

  4. .net程序员书单

    C# 基础 <CLR via C#> <c# 高级编程> 框架学习 <WPF编程宝典 > (英文名:<Pro WPF 4.5 in C#. Windows P ...

  5. sql server中的 trimtrailingblanks

    使用sp_help 查出 发现有个这个属性, 如何修改呢? SET ANSI_PADDING ONAlter Table Sys_users_History Alter   column PveSit ...

  6. mongodb 备份脚本

    ###############备份脚本#!/bin/bash basepath="/data/backup/dump$(date +%Y%m%d%H%M%S)" if [ ! -d ...

  7. 【ocp-12c】最新Oracle OCP-071考试题库(44题)

    44.(9-12)choose all that apply View the Exhibit and examine the details of the ORDER_ITEMS table. Ev ...

  8. “全栈2019”Java异常第十七章:Error详解

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

  9. elasticsearch-5.1.1 安装的问题

    elasticsearch 5.1 安装过程中遇到了一些问题做一些记录. 问题一:警告提示 [2016-12-20T22:37:28,543][INFO ][o.e.b.BootstrapCheck ...

  10. 定期删除Azure存储账号下N天之前的数据文件-ASM

    ######RemoveStorageBlob*DaysOld##### <# .SYNOPSIS Remove all blob contents from one storage accou ...