剑指offer有python版吗_剑指Offer算法类题目[Python版]
標簽:重復???作用???coding???面試???medium???mba???none???fas???utf-8
面試題012 數值的整數次方
解題思路1 考慮所有情況,循環連乘
代碼:
def power(base, exponent):
if (base==0):
return 0
if (exponent == 0):
return 1
res = 1
for i in range(abs(exponent)): #i只是循環變量,不起作用
res *= base
return 1/res if exponent
解題思路2 快速冪快速冪詳解
代碼
def fast_power(self, base, exponent):
if base == 0:
return 0
if exponent == 0:
return 1
e = abs(exponent)
tmp = base
res = 1
while(e > 0):
#如果最后一位為1,那么給res乘上這一位的結果
if (e & 1 == 1):
res =res * tmp
e = e >> 1
tmp = tmp * tmp
return res if exponent > 0 else 1/res
面試題 順時針打印矩陣
題目描述:輸入一個矩陣,按照從外向里以順時針的順序依次打印出每一個數字,例如,如果輸入如下4 X 4矩陣: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 則依次打印出數字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
解題思路:打印第一行 然后刪除第一行 剩余部分逆時針轉動90度。重復以上步驟,直到矩陣為空
代碼
# -*- coding:utf-8 -*-
class Solution:
def printMatrix(self, matrix):
# write code here
s=[]
while matrix:
s+=matrix[0]
del matrix[0]
matrix=zip(*matrix)[::-1]
return s
劍指Offer算法類題目[Python版]
標簽:重復???作用???coding???面試???medium???mba???none???fas???utf-8
總結
以上是生活随笔為你收集整理的剑指offer有python版吗_剑指Offer算法类题目[Python版]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 操作ps_使用Python
- 下一篇: python分类预测降低准确率_pyth