快速幂:二分的思想:底数每次^2, 幂次每次除2
class Solution:
def myPow(self, x: float, n: int) -> float:
if x==0:return 0
res = 1
if n<0: x, n = 1/x, -n
while n:
if n&1:res*=x
x = x*x
n = n>>1
return res
快慢指针:
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
fast, slow = head, head
pre = head
for _ in range(n):fast = fast.next
while fast:
fast = fast.next
pre = slow
slow = slow.next
if slow is head:return head.next
pre.next = slow.next
return head