python 算法面试题

1、题目是:有一组“+”和“-”符号,要求将“+”排到左边,“-”排到右边,写出具体的实现方法。

def StringSort(data):
    startIndex=0
    endIndex=0
    count=len(data)
    while startIndex+endIndex<count:
        if data[startIndex]=='-':
            data[startIndex],data[count-endIndex-1]=data[count-endIndex-1],data[startIndex]
            endIndex+=1
        else:
            startIndex+=1
    return data
data=['-','-','+','-','+','+','-','+','+','-','-','+','-']
print(StringSort(data))

猜你喜欢

转载自www.cnblogs.com/sui776265233/p/10050853.html