관리 메뉴

솜씨좋은장씨

[leetCode] 50. Pow(x, n) (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 50. Pow(x, n) (Python)

솜씨좋은장씨 2020. 6. 2. 17:42
728x90
반응형

Implement pow(x, n), which calculates x raised to the power n (x^n).

 

Example 1:

Input: 2.00000, 10
Output: 1024.00000

Example 2:

Input: 2.10000, 3
Output: 9.26100

Example 3:

Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25

Note:

  • -100.0 < x < 100.0
  • n is a 32-bit signed integer, within the range [−2^31, 2^31 − 1]

Solution

class Solution:
    def myPow(self, x: float, n: int) -> float:
        my_pow_num = pow(x, n)
        
        if my_pow_num > (pow(2, 31) - 1):
            my_pow_num = pow(2, 31) -1
        elif my_pow_num < (pow(2, 31)) * (-1):
            my_pow_num = pow(2, 31) * (-1)
            
        return my_pow_num

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments