关于这个执行的效率问题,同样的代码每次执行都有差别,所以不用在意
class Solution(object):
"""
344. 反转字符串
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
"""
def reverseString1(self, s):
"""
:type s: List[str]
:rtype: Non
"""
s[:] = s[::-1]
return s
def reverseString(self, s):
"""
:type s: List[str]
:rtype: Non
"""
length = len(s)
i = 0
j = length - 1
while i < j:
s[i], s[length - 1 - i] = s[length - 1 - i], s[i]
i += 1
j -= 1
return s
def reverseString2(self, s):
"""
:type s: List[str]
:rtype: None Do not return anything, modify s in-place instead.
"""
for i in range(len(s) >> 1):
s[i] = chr(ord(s[i]) + ord(s[-1 - i]))
s[-1 - i] = chr(ord(s[i]) - ord(s[-1 - i]))
s[i] = chr(ord(s[i]) - ord(s[-1 - i]))
def reverseString3(self, s):
"""
:type s: List[str]
:rtype: None Do not return anything, modify s in-place instead.
"""
t = ' '
for i in range(len(s) // 2):
t = ord(s[i])
s[i] = s[-1 - i]
s[-1 - i] = chr(t)
if __name__ == '__main__':
s1 = Solution()
nums = ["h","e","l","l","o"]
print(s1.reverseString(nums))