题目:
给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。
注意事项:
小写字母或者大写字母他们之间不一定要保持在原始字符串中的相对位置。
样例:
样例
给出"abAcD",一个可能的答案为"acbAD"
刚开始以为要正规排序后来发现其实挺简单的,用冒泡排序解决了。
上代码:
class Solution:
"""
@param: chars: The letter array you should sort by Case
@return: nothing
"""
def sortLetters(self, chars):
# write your code here
if(not chars):
return chars
temp = chars[0]
for i in range(len(chars)-1):
for j in range(len(chars)-i-1):
if(ord(chars[j])<91 and ord(chars[j])>64):
if(ord(chars[j+1])<123 and ord(chars[j+1])>96):
temp = chars[j]
chars[j] = chars[j+1]
chars[j+1] = temp
大概这样…小写字母挪到前面就行