题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
import sys #for line in sys.stdin: # a = line.split() # print(int(a[0]) + int(a[1])) s=input() stack1 = [] stack2 = [] stack3 = [] for i in s: if i.isalpha(): stack1.append(i) else: stack2.append(i) #print(stack1,stack2) #排序,根据x的大写排序,不改变stack中x的值 stack1 = sorted(stack1,key=lambda x :x.upper()) #print(stack1) #将列表反转后,pop函数会从最后一个元素开始输出 stack1=stack1[::-1] stack2=stack2[::-1] #当i为字母时,从原来的栈中取元素 #否则当i不为字母时,从stack2中取元素 for i in s: if i.isalpha(): stack3.append(stack1.pop()) else: stack3.append(stack2.pop()) print("".join(stack3))