今天的題目:提取不重復的整數
輸入一個int型整數,按照從右向左的閱讀順序,返回一個不含重復數字的新的整數。
首先對于實現逆序,有以下幾個方法:
字符串逆序
1、
strA[::-1]
2、
#coding=utf-8
strA = raw_input("請輸入需要翻轉的字符串:")
order = []
for i in strA:
order.append(i)
order.reverse() #將列表反轉
print ''.join(order) #將list轉換成字符串
我在一開始想到的方法就是通過將數字放入list然后進行逆序排序,set實現去重,其實是不可以的,因為使用set之后的順序就打亂了。
參考了一下其他人的代碼:
其中一個高手很好的解決了我的問題,而且代碼很簡單,對set集合排序,使用了原list的索引
a = str(input())[::-1]
print ''.join(sorted(set(a),key=a.index))
# 這個代碼在網站上顯示的內存占用為0,而第二段代碼占用為140K
n=raw_input()
l=list(reversed(n))
result=[]
for i in l:
if i not in result:
result.append(i)
print("".join(result))
n=raw_input()
l=(reversed(n))
result=[]
for i in l:
if i not in result:
result.append(i)
print (''.join(result))