華爲校招(字符集合)python

題目描述
輸入一個字符串,求出該字符串包含的字符集合
輸入描述:
每組數據輸入一個字符串,字符串最大長度爲100,且只包含字母,不可能爲空串,區分大小寫。
輸出描述:
每組數據一行,按字符串原有的字符順序,輸出字符集合,即重複出現並靠後的字母不輸出。
輸入例子:
abcqweracb
輸出例子:
abcqwer

分析:從左向右依次遍歷,邊檢測邊驗證
python實現如下:
#-*-coding:UTF-8-*-
s = raw_input()
a = []
for i in range(len(s)):
    if s[i] not in a:
        a.append(s[i])#從左到右遍歷字符串,
        # 如果字符串s中的元素沒有在a中的時候, 那麼就添加進去
stra = ''.join(a)#將列表轉化爲字符串
print stra

輸出結果示例:
yuantian
yuanti

Process finished with exit code 0

abcqweracb
abcqwer

Process finished with exit code 0



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章