344.文字列の反転

4539 ワード

           。           char[]      。

, 、 O(1) 。

ASCII 。



:["h","e","l","l","o"]
:["o","l","l","e","h"]



Python 。
, 。
 1 class Solution(object):
 2     def reverseString(self, s):
 3         """
 4         :type s: List[str]
 5         :rtype: None Do not return anything, modify s in-place instead.
 6         """
 7         return s[::-1]
 8 
 9     def reverseString2(self, s):
10         print(type(s), type(s[0]), s)
11         print(len(s))
12         i, j = 0, len(s) - 1
13         while i < j:
14             s[i], s[j] = s[j], s[i]
15             i += 1
16             j -= 1
17         # print(type(s), type(s[0]), s)
18         return s
19 
20 
21 if __name__ == '__main__':
22     solution = Solution()
23     print(solution.reverseString(["h", "e", "l", "l", "o"]))