今天的題目:提取不重復(fù)的整數(shù)
輸入一個(gè)int型整數(shù),按照從右向左的閱讀順序,返回一個(gè)不含重復(fù)數(shù)字的新的整數(shù)。
首先對(duì)于實(shí)現(xiàn)逆序,有以下幾個(gè)方法:
字符串逆序
1、
strA[::-1]
2、
#coding=utf-8
strA = raw_input("請(qǐng)輸入需要翻轉(zhuǎn)的字符串:")
order = []
for i in strA:
order.append(i)
order.reverse() #將列表反轉(zhuǎn)
print ''.join(order) #將list轉(zhuǎn)換成字符串
我在一開始想到的方法就是通過將數(shù)字放入list然后進(jìn)行逆序排序,set實(shí)現(xiàn)去重,其實(shí)是不可以的,因?yàn)槭褂胹et之后的順序就打亂了。
參考了一下其他人的代碼:
其中一個(gè)高手很好的解決了我的問題,而且代碼很簡(jiǎn)單,對(duì)set集合排序,使用了原list的索引
a = str(input())[::-1]
print ''.join(sorted(set(a),key=a.index))
# 這個(gè)代碼在網(wǎng)站上顯示的內(nèi)存占用為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))