Write a function that reverses a string. The input string is given as an array of characters s.
- Constraints
1. 1 <= s.length <= 10^5
2. s[i] is a printable ascii character
- Follow up: Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
1 ) 투 포인터를 이용해서 스왑
class Solution {
public void reverseString(char[] s) {
int l=0,r=s.length-1;
char temp;
while(l<r){
temp=s[l];
s[l]=s[r];
s[r]=temp;
l++;
r--;
}
}
}
참고 : 파이썬 알고리즘 인터뷰
'알고리즘과 자료구조 > OJ' 카테고리의 다른 글
LeetCode 49. Group Anagrams (0) | 2021.09.18 |
---|---|
LeetCode 819. Most Common Word(StringBuilder 이용) (0) | 2021.09.12 |
LeetCode 937. Reorder Data in Log Files(Lamda식을 이용한 sort) (0) | 2021.09.04 |
LeetCode 125. Valid Palindrome, java (0) | 2021.08.29 |