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))
    
    
  • 相关阅读:
    Android 70道面试题汇总不再愁面试
    TOMCAT用Https替换Http的方法
    Struts2+Spring3+Hibernate3配置全过程
    javac 无效标签
    hibernate
    数据库命令行启动
    Tomcat检测程序
    SQL
    Unsupported major.minor version 49.0的错误解决
    ImportError: No module named pysqlite2
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/14210532.html
Copyright © 2011-2022 走看看