本文主要是介绍LeetCode | 344.反转字符串,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
设置头尾两个指针,依靠中间变量temp交换头尾指针所指元素,头指针后移,尾指针前移,直到头尾指针重合或者头指针在尾指针后面一个元素
class Solution(object):def reverseString(self, s):""":type s: List[str]:rtype: None Do not return anything, modify s in-place instead."""temp = 0i,j = 0,len(s)-1while i<j:temp = s[i]s[i] = s[j]s[j] = tempi += 1j -= 1if j == i-1 or i == j:return sclass Solution(object):def reverseString(self, s):""":type s: List[str]:rtype: None Do not return anything, modify s in-place instead."""return s.reverse()
这篇关于LeetCode | 344.反转字符串的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!