zoukankan      html  css  js  c++  java
  • 344. 反转字符串

    将输入的字符串反转过来。输入字符串以字符数组 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"]))
    
    
    
     
  • 相关阅读:
    hdu 4474 转化为bfs + 一个巧妙的剪枝~
    数据结构几类排序的总结和完整代码 待续。。
    poj 2135 Farm Tour
    hdu 4374 (单调队列+dp)
    poj2391 Ombrophobic Bovines 拆点连边要注意
    hdu3507
    hdu1506
    poj2175
    poj3308
    poj3155 Hard Life
  • 原文地址:https://www.cnblogs.com/panweiwei/p/12682111.html
Copyright © 2011-2022 走看看