Today do Newcode ' histogram within the maximum rectangle ' problem, the pit daddy string operation toss a long time, specially to summarize under. Topic for (http://www.nowcoder.com/practice/13ba51c3fec74b58bbc8fa8c3eedf877?tpId=49&tqId=29284&rp=1&ru=/ ta/2016test&qru=/ta/2016test/question-ranking)
First put the code, the results of this machine Python3.4 run no problem, Newcoder is Python2.7, error, not resolved:%d format:a number is required, not nonetype
#-*-coding:utf-8-*-classMaxinnerrec:defCountarea (self, A, n):#Write code hereL = [] forIiinchRange (n-1, 0, 1): forJjinchRange (0, ii):ifA[JJ] > A[JJ + 1]: #tmp = A[JJ] #A[JJ] = a[jj + 1] #A[JJ + 1] = A[jj]A[JJ],A[JJ + 1] = A[JJ + 1],A[JJ]PrintA#after sorting is complete forIinchrange (N): L.append (A[i]* (N-i))#Calculate Area forIiinchrange (n):ifL[0] <L[ii]: l[0], L[ii]=L[ii], l[0]Print(l)#Sort an area Print(l[0])if __name__=="__main__": A=[] SS=input () AA= Ss.replace ('[',' ') AA= Aa.replace (']',' ') AA= Aa.split (',') n= Int (aa[-1]) forIinchRange (int (aa[-1]): a.append (int (aa[i) ))Print(A)#A = [2,7,9,4,1] #n = 5CH=Maxinnerrec () Ch.countarea (a,n)
Body: string manipulation
Replace: string substitution
SS = [2,7,9,4,1= ss.replace ('[','') #SS String All ' [' Use ' the ' space instead of AA = 2,7,9,4,1]
Split: String splitting
A = ' 2,7,9,4,1 ' #A is a string of AA = aa.split (',') # a to make a a string type of list
When split has multiple delimiters, it is necessary to use regular expressions:
Importres1= [8,4,6,2,5,9,1],7S2=re.split (R'[\[\s\]\,]+', S1)#remove [, Space,],+ to represent multiple symbolsPrint(S2) ["','8','4','6','2','5','9','1','7']#Why is there a space in front of you that can't go away
Join: String connection
A = ['a','b','c',' D ' ] ". Join (a)print contentabcd
Add again next time:
Python string manipulation