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

    关于这个执行的效率问题,同样的代码每次执行都有差别,所以不用在意
    
    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))
    
    
  • 相关阅读:
    三层架构补充
    复习三层架构
    复习DOM、JQuery
    复习HTML CSS JavaScript
    Git在新电脑拉github 上的项目
    超全的IE兼容性问题及解决方案
    JS操作iframe
    attachEvent和addEventListener
    HTTP 方法:GET 对比 POST
    原生JS+ CSS3创建loading加载动画;
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/14210532.html
Copyright © 2011-2022 走看看