Day-5 Leetcode Coding Journey: Mastering Problem-Solving Techniques
Leetcode #50 Pow(x, n)
📌 Problem Statement
Implement the function mypow(x, n), which calculates x^n (x raised to the power of n).
Constraints
−100.0<x<100.0
−2^31≤n≤2^31−1
x^n will be within the range of a 32-bit signed floating-point number.
🚀 Code Implementation
class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if n==0:
return 1
if n<0:
x=1/x
n=-n
result=1
while n>0:
if n%2==1:
result=result*x
x=x*x
n//=2
return result
Leetcode #53 Maximum Subarray
📌 Problem Statement
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Constraints
1≤nums.length≤10^5
−10^4≤nums[i]≤10^4
🚀 Code Implementation
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max_sum=nums[0]
current_sum=nums[0]
for i in range(1,len(nums)):
current_sum=max(nums[i],current_sum+nums[i])
max_sum=max(max_sum,current_sum)
return max_sum
Â