leetcode 372. Super Pow 快速幂实现
来源:互联网 发布:阿里云解析新网域名 编辑:程序博客网 时间:2024/05/16 16:22
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
这道题就是实现幂运算,但是不会做,网上看到了一个做法,很不错。
代码如下:
//快速幂,涉及到数学知识,不会做public class Solution{ public int superPow(int a, int[] b) { if(a == 1 || b.length == 0) return 1; a = a % 1337; int[] nums = new int[11]; int prev = 1,i = 0; while(i <= 10){//至于为什么是10,因为更多的话会浪费空间,更小的话,会增加递归层数,至于10是否最优,没有测量。 nums[i++] = prev; prev = prev * a % 1337; } int[] divisor = new int[b.length - 1]; int remainder = divide(b,10,divisor); return superPow(nums[10],divisor)*nums[remainder]%1337; } private int divide(int[] num,int baseNum,int[] divisor) { System.arraycopy(num,0,divisor,0,divisor.length); return num[num.length - 1]; }}
阅读全文
0 0
- leetcode 372. Super Pow 快速幂实现
- Leetcode 372. Super Pow 快速幂计算 解题报告
- LeetCode 372. Super Pow (快速幂取模)
- LeetCode Super Pow(快速求幂算法)
- leetcode.372. Super Pow
- LeetCode 372. Super Pow
- [LEETCODE] 372. super pow
- LeetCode--372. Super Pow
- leetcode 372. Super Pow
- Leetcode 372. Super Pow
- [leetcode] 372. Super Pow
- Leetcode: 372.Super Pow
- 372. Super Pow(LeetCode)
- LeetCode 372. Super Pow
- [LeetCode]372. Super Pow
- [leetcode]372. Super Pow
- leetcode 372. Super Pow
- LeetCode—372. Super Pow
- bzoj 4936: [Ceoi2016]match
- stm32f103 spi slave从机模式miso需要上拉
- Recover the Smallest Number (30)
- 使用ViewPager实现屏幕滑动
- lambda表达式杂谈
- leetcode 372. Super Pow 快速幂实现
- String类型转化成Int类型
- 关于用户 root密码忘记时的密码重制
- logback的使用和logback.xml详解
- linux内核驱动模块开发makefile实例解析
- construct2 游戏制作
- (完)learn python第五章:更多类 (第六节:递推式构造列表list comprehensions)
- 动态代理-增强方法
- C 语言 简单桶排序 算法&实现