#version 1, 1 line
class Solution:
def maximum69Number (self, num: int) -> int:
return str(num).replace('6','9',1)
#version 2, from others work in leetcode
class Solution:
def maximum69Number (self, num: int) -> int:
str_num = str(num)
if '6' in str_num:
pos = str_num.index('6')
list_num = list(str_num)
list_num[pos] = '9'
str_num = ''.join(list_num)
return int(str_num)
else:
return num
#version 3, from others work in leetcode
class Solution:
def maximum69Number (self, num: int) -> int:
s = str(num)
lst = []
for i in s:
lst.append(i)
for i in range(len(lst)):
if lst[i] == '6':
lst[i] = '9'
break
s = ''.join(lst)
return int(s)
1323
最新推荐文章于 2024-07-15 18:04:03 发布