String Summary
151. Reverse Words in a String
def reverseWords_02(self, s):
"""
:type s: str
:rtype: str
"""
def revS(start, end):
while start < end:
s[start], s[end] = s[end], s[start]
start, end = start + 1, end - 1
s = s.strip()
s = list(s)
space = 0
count = 0
for i in range(len(s)):
if s[i] == " " and space < 1:
space += 1
s[count] = s[i]
elif s[i] == " ":
continue
else:
space = 0
s[count] = s[i]
count += 1
s = list(s[:count])[::-1]
start = 0
for i in range(len(s)):
if s[i] == " ":
revS(start, i - 1)
start = i + 1
elif i == len(s) - 1:
revS(start, i)
return ''.join(s)
125. Valid Palindrome
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
alphanumeric = [chr(i) for i in range(65,91)] + [chr(i).lower() for i in range(65,91)] + [str(i) for i in range(10)]
start, end = 0, len(s) - 1
while start < end:
while start < len(s) and s[start] not in alphanumeric:
start += 1
while end >= 0 and s[end] not in alphanumeric:
end -= 1
if start >= len(s) and end < 0:
break
if s[start].lower() == s[end].lower():
start += 1
end -= 1
else:
return False
return True
171. Excel Sheet Column Number
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
j = 0
for i in range(len(s) - 1, -1, -1):
result += (ord(s[i]) - 64) * 26 ** j
j += 1
return result
168. Excel Sheet Column Title
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
res = ""
d = {}
for i in range(26):
d[i] = chr(i+65).upper()
while n:
cur = (n - 1) % 26
res = d[cur] + res
n = (n - 1) / 26
return res
17. Letter Combinations of a Phone Number
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
keyMap = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
def dfs(pool, temp):
if len(temp) == len(digits):
result.append(''.join(temp))
return
for i in keyMap[pool[0]]:
dfs(pool[1:], temp + [i])
if not digits:
return []
result = []
dfs(digits, [])
return result
38. Count and Say
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
res = '1'
for i in xrange(n-1):
new_res = ''
j = 0
while j < len(res):
count = 1
while j < len(res)-1 and res[j] == res[j+1]:
j = j + 1
count = count + 1
j = j + 1
new_res = new_res + str(count) + res[j-1]
res = new_res
return res
def reverseWords_02(self, s):
"""
:type s: str
:rtype: str
"""
def revS(start, end):
while start < end:
s[start], s[end] = s[end], s[start]
start, end = start + 1, end - 1
s = s.strip()
s = list(s)
space = 0
count = 0
for i in range(len(s)):
if s[i] == " " and space < 1:
space += 1
s[count] = s[i]
elif s[i] == " ":
continue
else:
space = 0
s[count] = s[i]
count += 1
s = list(s[:count])[::-1]
start = 0
for i in range(len(s)):
if s[i] == " ":
revS(start, i - 1)
start = i + 1
elif i == len(s) - 1:
revS(start, i)
return ''.join(s)
125. Valid Palindrome
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
alphanumeric = [chr(i) for i in range(65,91)] + [chr(i).lower() for i in range(65,91)] + [str(i) for i in range(10)]
start, end = 0, len(s) - 1
while start < end:
while start < len(s) and s[start] not in alphanumeric:
start += 1
while end >= 0 and s[end] not in alphanumeric:
end -= 1
if start >= len(s) and end < 0:
break
if s[start].lower() == s[end].lower():
start += 1
end -= 1
else:
return False
return True
171. Excel Sheet Column Number
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
j = 0
for i in range(len(s) - 1, -1, -1):
result += (ord(s[i]) - 64) * 26 ** j
j += 1
return result
168. Excel Sheet Column Title
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
res = ""
d = {}
for i in range(26):
d[i] = chr(i+65).upper()
while n:
cur = (n - 1) % 26
res = d[cur] + res
n = (n - 1) / 26
return res
17. Letter Combinations of a Phone Number
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
keyMap = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
def dfs(pool, temp):
if len(temp) == len(digits):
result.append(''.join(temp))
return
for i in keyMap[pool[0]]:
dfs(pool[1:], temp + [i])
if not digits:
return []
result = []
dfs(digits, [])
return result
38. Count and Say
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
res = '1'
for i in xrange(n-1):
new_res = ''
j = 0
while j < len(res):
count = 1
while j < len(res)-1 and res[j] == res[j+1]:
j = j + 1
count = count + 1
j = j + 1
new_res = new_res + str(count) + res[j-1]
res = new_res
return res
留言
張貼留言