GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode_python/python/050_Pow(x, n).py at master · vishwamitrakala/leetcode_python

Expand file tree

/

050_Pow(x, n).py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
31 lines (30 loc) · 811 Bytes
/

050_Pow(x, n).py

Copy path

File metadata and controls

31 lines (30 loc) · 811 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
# def myPow(self, x, n):
# """
# :type x: float
# :type n: int
# :rtype: float
# """
# if n == 0:
# return 1
# temp = pow(x, n / 2)
# if n % 2 == 0:
# return temp * temp
# else:
# return temp * temp * x
def myPow(self, x, n):
# https://leetcode.com/discuss/93413/iterative-log-n-solution-with-clear-explanation
# 9 = 2^3 + 2^0 = 1001
# x^9 = x^(2^3)*x(2^0)
# multiple x^i when i place is 1
if n == 0:
return 1
res ,curr = 1, abs(n)
while curr > 0:
if curr & 1 == 1:
res *= x
curr >>= 1
x *= x
if n < 0:
return 1 / res
return res
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.