TreeviewCopyright © aleen42 all right reserved, powered by aleen42

344. 反转字符串

https://leetcode-cn.com/problems/reverse-string/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-07-22 18:03:18
 * @Description: https://leetcode-cn.com/problems/reverse-string/
 * @FilePath: \leetcode-googtech\#344. Reverse String\Solution.java
 */ 
class Solution {
    // 双指针法
    public void reverseString(final char[] s) {
        char temp;
        int left = 0,right = s.length - 1;
        while(left < right) {
            temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
    }
}

Python

'''
@Author: Goog Tech
@Date: 2020-07-22 18:03:26
@Description: https://leetcode-cn.com/problems/reverse-string/
@FilePath: \leetcode-googtech\#344. Reverse String\Solution.py
'''
class Solution(object):
    # 双指针法
    def reverseString(self, s):
        """
        :type s: List[str]
        :rtype: None Do not return anything, modify s in-place instead.
        """
        left, right = 0, len(s) - 1
        while left < right:
            s[left], s[right] = s[right], s[left]
            left += 1
            right -= 1
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""