将字符串数字中为偶数位的数字进行翻转,将翻转后的结果进行输出。
class Solution: def change(self , number ): # write code here if len(number)==0: return number i=0 j=len(number)-1 lst = list(map(int, number)) while(i<j): if(j<0&nbs***bsp;i> len(lst)): break while i<j and lst[i]%2 != 0 and i < len(lst): i+=1 while i<j and lst[j]%2 != 0 and j > 0: j-=1 lst[i],lst[j] =lst[j],lst[i] i+=1 j-=1 string = "" for i in lst: string = string+str(i) return string
class Solution: def change(self , number ): sta = [] j = 0 nums = list(map(int, number)) for i in range(len(nums)): if nums[i]%2 == 0: sta.append(nums[i]) nums[i]='0' sta.reverse() for i in range(len(nums)): if nums[i] == '0': nums[i] = sta[j] j = j + 1 return ''.join(map(str,nums))