Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- 코딩
- 알고리즘
- 탐욕알고리즘
- LeetCode
- greedy
- javascript
- sliding window
- two pointers
- 그리디
- it
- 방송대
- 방송통신대학교
- 방통대
- 깃
- Git
- 컴퓨터과학과
- algorithm
- java
- DP
- 리트코드
- boj
- 이진탐색
- 자바
- 투포인터
- 코테
- 백준
- Binary Search
- 자바스크립트
- dynamic programming
- 완전탐색
Archives
- Today
- Total
개발이 취미인 주니어 기획자
[투포인터][JavaScript][LeetCode] #344. Reverse String 본문
728x90
반응형
#투포인터 #EASY
Reverse String - LeetCode
Can you solve this real interview question? Reverse String - Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algo
leetcode.com
🌷 문제 설명
✏️ LeetCode 연습문제: Reverse String
Write a function that reverses a string. The input string is given as an array of characters s.
You must do this by modifying the input array in-place with O(1) extra memory.
🎃 제한 사항
1 <= s.length <= 10^5
s[i] is a printable ascii character.
입출력 예
| s | result |
| ["h","e","l","l","o"] | ["o", "l", "l", "e", "h"] |
| ["H","a","n","n","a","h"] | ["h","a","n","n","a","H"] |
🌷 내 코드
1. 투포인터

var reverseString = function(s) {
let left = 0; // 첫 요소
let right = s.length-1; // 마지막 요소
while (left < right) {
let temp = s[left]
s[left] = s[right]
s[right] = temp // switch!
left++ // 다음으로 넘어가기
right--
}
};
2. reverse()

var reverseString = function(s) {
s.reverse()
};
🌷 코멘트
첨에 while 조건을 (left < Math.floor(s.length/2))로 주고는 투포인터를 썼는데 reverse()보다 느려서 아니 이럴거면 왜 이걸 쓰는걸까 진지하게 고민했던 내가 유머
하지만 나는 (특히 알고리즘에 관해서 만큼은) 나를 믿지 않기에... 다시 보고 고쳤더니 예상하던 런타임 달성 🎉
블로그 내용에 문제가 있다면 댓글 혹은 아래로 연락주세요!
~대가리 꽃밭인 디지털 노마드가 꿈이예요~
🧚♀️ Gyumin Lee
📧 gyumin.q.lee@gmail.com
qminlee723 - Overview
noob. qminlee723 has 8 repositories available. Follow their code on GitHub.
github.com
728x90
반응형
'문제 풀이 > 알고리즘 문제 풀이' 카테고리의 다른 글
| [투포인터][JavaScript][LeetCode] #1480. Running Sum of 1d Array (0) | 2023.04.02 |
|---|---|
| [투포인터][JavaScript][LeetCode] #557. Reverse Words in a String III (0) | 2023.04.01 |
| [투포인터][JavaScript][LeetCode] #283. Move Zeros (0) | 2023.03.31 |
| [투포인터][JavaScript][LeetCode] #977. Squares of a Sorted Array (0) | 2023.03.30 |
| [이진탐색][JavaScript][LeetCode] #35. Search Insert Position (0) | 2023.03.30 |