当前位置:网站首页>Leetcode skimming - super power 372 medium
Leetcode skimming - super power 372 medium
2022-07-28 19:05:00 【Fire breathing dragon and water arrow turtle】
Super power thinking and source code
The title of super power is shown in the figure below , This problem belongs to mathematics and divide and conquer , It mainly examines the use of recursive methods and the understanding of mathematical ideas of the topic . The title of this article, the author thought 2 Methods , They are reverse order traversal method and positive order traversal method , The reverse traversal method uses Java Compiling , The positive order traversal method uses Python Compiling , Of course, this may not be the optimal solution , I also hope you guys can give a faster algorithm .
I think this problem can be solved by the idea of reverse traversal , First, implement a function that iterates in reverse order to calculate the power , The function is initialized internally , Then judge whether the current incoming number is 0, If not for 0 Then start traversing , Judge whether the number is odd , If it is an odd number, the numbers are iterated in reverse order , Until the traversal is over and the result is returned . And in the main function , Initialize a number first , Then start traversing the loop , After calling the function to calculate the power and the current historical results, calculate and perform the remainder operation , Until the loop traversal ends , Return the final result to . Then according to this idea, our Java The code is as follows :
# Fire breathing dragon and water arrow turtle
class Solution {
static final int IND = 1337;
public int superPow(int a, int[] b) {
int res = 1;
for (int ir = b.length - 1; ir >= 0;ir--) {
res = (int) ((long) res * pow(a, b[ir]) % IND);
a = pow(a, 10);
}
return res;
}
public int pow(int x, int n) {
int vex = 1;
while (n != 0) {
if (n % 2 != 0) {
vex = (int) ((long) vex * x % IND);
}
x = (int) ((long) x * x % IND);
n = n/2;
}
return vex;
}
}

obviously , The effect of our reverse traversal method is general , At the same time, it can also be solved by positive order traversal . Initialize the parameters first , Then start traversing the loop , Call the result directly to the power function of the system for iterative positive order calculation , Until the end of the final cycle and return the result . So according to this idea, we can solve , Here is Python Code :
# Fire breathing dragon and water arrow turtle
class Solution:
def superPow(self, a: int, b: List[int]) -> int:
NUM = 1337
res = 1
for jr in b:
res = pow(res, 10, NUM) * pow(a, jr, NUM) % NUM
return res

As a result Java The efficiency of version reverse traversal method is general , and Python The speed of the positive sequence traversal method of version is also relatively general , But there should be more ways to further speed up , I hope friends can give me more advice , Thank you very much .
边栏推荐
- Example of observer mode of C -- ordering milk
- N32 replaces STM32. Don't ignore these details!
- How to solve the problem that easycvr device cannot be online again after offline?
- What if you don't understand the difference between modularity, componentization and plug-in?
- 历史上的今天:微软收购 QDOS;模型检测先驱出生;第一张激光照排的中文报纸...
- Why app uses JSON protocol to interact with server: serialization related knowledge
- LeetCode_ 1137_ Nth teponacci number
- The wechat installation package has expanded 575 times in 11 years, and the up owner: "98% of the documents are garbage"; Apple App store was exposed to a large number of pornographic apps; Four techn
- Configuration tutorial: how does the organizational structure of the new version of easycvr (v2.5.0) cascade to the superior platform?
- Getting started with QT & OpenGL
猜你喜欢

现代化个人博客系统 ModStartBlog v5.4.0 登录界面改版,新增联系方式

Kali doesn't have an eth0 network card? What if you don't connect to the Internet

kotlin:Nothing

Unity 之 切换语言导致报错:System.FormatException:String was not recognized as a valid DateTime.

Introduction and advanced level of MySQL (I)

视频融合云服务EasyCVR平台白名单功能如何使用?

kotlin:Nothing

EasyCVR新版本级联时,下级平台向上传递层级目录显示不全的原因分析

Use the self-developed proxy server to solve the cross domain access errors encountered when uploading files by SAP ui5 fileuploader trial version

uwb模块实现人员精确定位,超宽带脉冲技术方案,实时厘米级定位应用
随机推荐
Can I get employed after two months of software testing training?
How big is it suitable for learning software testing?
Swiftui component how to implement textfield of hidden part of phone number mask (tutorial includes source code)
三分钟了解快来新媒体
What if svchost.exe of win11 system has been downloading?
我的创作纪念日 -- 2022年7月25日
How to adjust the brightness of win11? Four methods of adjusting screen brightness in win11
unity CS1513
2022杭电多校第二场1011 DOS Card(线段树)
历史上的今天:微软收购 QDOS;模型检测先驱出生;第一张激光照排的中文报纸...
QT user defined control user guide (flying Qingyun)
Xiaobai must see the development route of software testing
New upgrade! The 2022 white paper on cloud native architecture was released
Three minutes to understand, come to new media
Introduction and advanced MySQL (7)
Pyqt5 rapid development and practice 5.3 multithreading
New progress in the implementation of the industry | the openatom openharmony sub forum of the 2022 open atom global open source summit was successfully held
How new people get started learning software testing
2022年牛客多校第2场 J . Link with Arithmetic Progression (三分+枚举)
Can the training software test be employed