class Solution: # @param s, a string # @return a list of strings def restoreIpAddresses(self, s): n=len(s) rst=[(0,[])] for i in range(4): newlist=[] for elem in rst: if (4-i)*3>=n-elem[0]: for k in range(1,4): if elem[0]+k<=n and (s[elem[0]]!='0' or k==1) and 0<=int(s[elem[0]:elem[0]+k])<=255: newlist.append((k+elem[0],elem[1]+[s[elem[0]:elem[0]+k]])) rst=newlist res=[] for elem in rst: if elem[0]==n: res.append('.'.join(elem[1])) return res
Thursday, September 18, 2014
Leetcode: Restore IP Addresses @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment