LeetCode "483. Smallest Good Base" !!
2017-01-31 07:35
330 查看
A more programming-like solution, is to hack the problem from simple: we try each possble base value, and see which 111..11 fits target number - using binary search.
However, as you expect, there's a smarter, math solution.
https://discuss.leetcode.com/topic/76368/python-solution-with-detailed-mathematical-explanation-and-derivation
class Solution(object): def helper(self, num, p): l = 1; r = int(pow(num, 1.0/p) + 1) # find highest possible bit while(l < r): mid = l + (r - l) // 2 sum = 0; cur = 1 # Addup to sum by form of 1111..11 for i in xrange(0, p + 1): sum += cur cur *= mid # Good? if sum == num: return mid elif sum > num: r = mid else: l = mid + 1 return -1 def smallestGoodBase(self, N): n = int(N) # iterate each base, from longest 1s to shortest for p in xrange(2, 101): if (1 << p) < n: k = self.helper(n, p) if k != -1: return str(int(k)) return str(n-1) # 11
However, as you expect, there's a smarter, math solution.
https://discuss.leetcode.com/topic/76368/python-solution-with-detailed-mathematical-explanation-and-derivation
相关文章推荐
- leetcode 483. Smallest Good Base 最小基数使为1 + 二分查找
- leetcode483. Smallest Good Base 的一些思考
- [Leetcode] 483. Smallest Good Base 解题报告
- leetCode 483. Smallest Good Base
- LeetCode 483 Smallest Good Base 题解
- LeetCode 483 Smallest Good Base 解题报告
- 483. Smallest Good Base
- [LeetCode] Smallest Good Base 最小的好基数
- leetcode题解-483. Smallest Good Base
- 483. Smallest Good Base
- Fixing "Base SDK Missing"
- <object height="500" align="middle" width="450" id="reader" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/
- FCKeditor 与 <base target="_self"/> 导至“无法打开Internet站点”错误
- <base target="_self"/>
- 【转】关于base target="_self" 等
- 关于SVN管理kernel source code时出错"找不到ipt_TCPMSS.h.svn-base"的问题
- "Base SDK Missing"问题的解决
- error LNK2001: 无法解析的外部符号 "public: virtual long __stdcall CBaseFilter
- 判断一个字符串中,"sunJavaibmjAvaoraclejaVahelloJAVAgoodjava" java 字符串出现的次数, 并把所有的 java改成大写或小写。
- MFC添加基于CListView创建新类时报"CListView: base class undefined"的解决