class Solution:
def maxDistance(self, colors: List[int]) -> int:
n = len(colors )
l, r = 0 ,n-1
while colors[r] == colors[l]:
r -= 1
ans = r-l
l, r = 0 ,n-1
while colors[l] == colors[r]:
l += 1
return max(ans,r-l)
链接: 942. 增减字符串匹配
按题意模拟:
class Solution:
def diStringMatch(self, s: str) -> List[int]:
l,r = 0,len(s)
ans = []
for c in s:
if c == 'I':
ans.append(l)
l += 1
else:
ans.append(r)
r -= 1
ans.append(l)
return ans
链接: 561. 数组拆分 I
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum (sorted(nums)[::2])
class Solution:
def maximum69Number (self, num: int) -> int:
t = list(str(num))
for i,c in enumerate(t):
if c == '6':
t[i] = '9'
break
return int(''.join(t))