python3的各种经典案例,总共299个案例,直接可以运⾏(前:100个案例)⼀.
⼆.
三.
第⼀章 难度等级★
【例1】反转⼀个3位整数难度等级★
3.代码实现
class Solution:
#参数number: ⼀个三位整数
#返回值: 反转后的数字
def reverseInteger(self, number):
h =int(number/100)
t =int(number%100/10)
z =int(number%10)
return(100*z+10*t+h)
#主函数
if __name__ =='__main__':
solution = Solution()
num =123
php环境搭建是什么意思ans = verseInteger(num)
print("输⼊:", num)
print("输出:", ans)
【例2】合并排序数组难度等级★
3.代码实现
#参数B: 有序整数数组B
#返回:⼀个新的有序整数数组
def mergeSortedArray(self, A, B):
i, j =0,0
C =[]
while i <len(A)and j <len(B):
if A[i]< B[j]:
C.append(A[i])
i +=1
else:
C.append(B[j])
j +=1
while i <len(A):
C.append(A[i])
i +=1
while j <len(B):
C.append(B[j])
j +=1
return C
#主函数
#主函数
if __name__ =='__main__':
A =[1,4]
B =[1,2,3]
D =[1,2,3,4]
E =[2,4,5,6]
solution = Solution()
print("输⼊:", A," ", B)
print("输出:", SortedArray(A,B))
print("输⼊:", D," ", E)
print("输出:", SortedArray(D,E))
【例3】旋转字符串难度等级★
3.代码实现
class Solution:
#参数s:字符列表
#参数offset:整数
#返回值:⽆
def rotateString(self, s, offset):
if len(s)>0:
offset = offset %len(s)
temp =(s + s)[len(s)- offset :2*len(s)- offset]
简单ps图片制作教程for i in range(len(temp)):
s[i]= temp[i]
#主函数
if __name__ =='__main__':
s =["a","b","c","d","e","f","g"]
offset =3
solution = Solution()
print("输⼊:s =",["a","b","c","d","e","f","g"]," ","offset =",offset) print("输出:s =", s)
【例4】相对排名难度等级★
3.代码实现
#返回列表
def findRelativeRanks(self, nums):
score ={}
for i in range(len(nums)):
score[nums[i]]= i
sortedScore =sorted(nums, reverse=True)
answer =[0]*len(nums)
for i in range(len(sortedScore)):
res =str(i +1)
if i ==0:
res ='Gold Medal'
if i ==1:
res ='Silver Medal'
if i ==2:
res ='Bronze Medal'
answer[score[sortedScore[i]]]= res
return answer
#主函数
if __name__ =='__main__':
num =[5,4,3,2,1]
s = Solution()
print("输⼊为:",num)
print("输出为:",s.findRelativeRanks(num))
【例5】⼆分查难度等级★
3.代码实现
class Solution:
#参数nums: 整数数组
#参数target: 要查的⽬标数字
#返回值:⽬标数字的第⼀个位置,从0开始
def binarySearch(self, nums, target):
return self.search(nums,0,len(nums)-1, target)
def search(self, nums, start, end, target):
if start > end:
return-1
mid =(start + end)//2
bottom的中文翻译
if nums[mid]> target:
return self.search(nums, start, mid, target)
if nums[mid]== target:
return mid
if nums[mid]< target:
return self.search(nums, mid, end, target)
#主函数
结构体的定义和方式if __name__ =='__main__':
my_solution = Solution()
nums =[1,2,3,4,5,6]
target =3
targetIndex = my_solution.binarySearch(nums, target) print("输⼊:nums =", nums," ","target =",target) print("输出:",targetIndex)
【例6】下⼀个更⼤的数难度等级★
3.代码实现
#参数nums2为整数数组
#返回整数数组
def nextGreaterElement(self, nums1, nums2):
answer ={}
stack =[]
for x in nums2:
while stack and stack[-1]< x:
answer[stack[-1]]= x
del stack[-1]
stack.append(x)
for x in stack:
answer[x]=-1
return[answer[x]for x in nums1]
#主函数
if __name__ =='__main__':
s = Solution()
nums1 =[4,1,2]
nums2 =[1,3,4,2]
print("输⼊1为:",nums1)
print("输⼊2为:",nums2)
print("输出为:",s.nextGreaterElement(nums1,nums2))【例7】字符串中的单词数难度等级★
3.代码实现
class Solution:
#参数s为字符串
#返回整数
def countSegments(self, s):
res =0
for i in range(len(s)):
if s[i]!=' 'and(i ==0or s[i -1]==' '):
res +=1
return res
#主函数
if __name__ =='__main__':
s = Solution()
n ="Hello, my name is John"
print("输⼊为:",n)
print("输出为:",s.countSegments(n))
【例8】信难度等级★
3.代码实现
"""
参数ransomNote为字符串
参数magazine为字符串
返回布尔类型
"""
def canConstruct(self, ransomNote, magazine):
arr =[0]*26
定义二维数组的格式for c in magazine:
arr[ord(c)-ord('a')]+=1
for c in ransomNote:
arr[ord(c)-ord('a')]-=1
if arr[ord(c)-ord('a')]<0:
return False
return True
#主函数
if __name__ =='__main__':
s = Solution()
ransomNote ="aa"
magazine ="aab"
print("输⼊信为:",ransomNote)
print("输⼊杂志内容:",magazine)
print("输出为:",s.canConstruct(ransomNote,magazine))【例9】不重复的两个数难度等级★
3.代码实现python基础代码实例
#参数arr是输⼊的待查数组
#返回值是两个值的列表,内容没有重复的
class Solution:
def theTwoNumbers(self, a):
ans =[0,0]
for i in a:
ans[0]= ans[0]^ i
c =1
while c & ans[0]!= c:
c = c <<1
for i in a:
if i & c == c:
ans[1]= ans[1]^ i
ans[0]= ans[0]^ ans[1]
return ans
if __name__ =='__main__':
arr =[1,2,5,1]
solution = Solution()
print(" 数组为:", arr)
print(" 两个没有重复的数字是:", solution.theTwoNumbers(arr))【例10】双胞胎字符串难度等级★
3.代码实现

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。