Leetcode344. 反转字符串(JAVA双指针)
作者:互联网
题目链接:https://leetcode-cn.com/problems/reverse-string/
解题思路
l
指针从左到右遍历,r
指针从右到左遍历,交换左右两个指针
代码
class Solution {
public void reverseString(char[] s) {
for (int l = 0, r = s.length - 1; l < r; ++l, --r) {
char t = s[l];
s[l] = s[r];
s[r] = t;
}
}
}
复杂度分析
- 时间复杂度:O(n)
- 空间复杂度:O(1)
标签:遍历,JAVA,cn,复杂度,Leetcode344,char,从右到左,指针 来源: https://blog.csdn.net/qq_44713772/article/details/117390871