LeetCode初級演算法問題(字串)
反轉字串
class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
return s[::-1]
顛倒整數
class Solution(object): def reverse(self, x): """ :type x: int :rtype: int """ sum = 0 if x < 0 : y = -x else : y = x while (y>0): sum = sum*10 + y%10 y = y/10 if sum > (2**31-1) or -sum < -2**31: return 0 else: if x < 0 : return -sum else: return sum
字串中的第一個唯一字元
class Solution:
def firstUniqChar(self, s):
"""
:type s: str
:rtype: int
"""
return min([s.index(ch) for ch in string.ascii_lowercase if s.count(ch) == 1] or [-1])