처음 돌렸을때는 16ms나왔는데 계속 돌릴수록 다른 Discuss 답안이랑 비슷한 28-32ms 나온다.
간단한 구조여서 어떻게 짜든 다 비슷하게 나오는듯.
내가 짠 코드
class Solution {
public:
void reverseString(vector<char>& s) {
char tmp;
for(int i=0;i<s.size()/2;i++){
tmp = s[i];
s[i] = s[s.size()-1-i];
s[s.size()-1-i] = tmp;
}
}
};
Success
Runtime: 16 ms, faster than 98.56% of C++ online submissions for Reverse String.
Memory Usage: 23.1 MB, less than 91.72% of C++ online submissions for Reverse String.
Discuss 코드 1
class Solution {
public:
void reverseString(vector<char>& s) {
int i = 0, j = s.size() - 1;
while(i < j){
swap(s[i++], s[j--]);
}
}
};
Success
Runtime: 32 ms, faster than 62.28% of C++ online submissions for Reverse String.
Memory Usage: 23.3 MB, less than 80.83% of C++ online submissions for Reverse String.
Discuss 코드 2
class Solution {
public:
void reverseString(vector<char>& s) {
reverse(s.begin() , s.end() );
}
};
Success
Runtime: 28 ms, faster than 77.16% of C++ online submissions for Reverse String.
Memory Usage: 23.2 MB, less than 91.72% of C++ online submissions for Reverse String.
leetcode.com/problems/reverse-string/
Reverse String - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
'Problem Solving > 리트코드' 카테고리의 다른 글
[LeetCode] Arithmetic SlicesSolution (0) | 2021.02.19 |
---|---|
[LeetCode] Linked List Cycle (0) | 2021.02.08 |
[LeetCode] Number of 1 Bits (0) | 2021.02.08 |
[LeetCode] Squares of a Sorted Array (0) | 2021.01.29 |
[LeetCode] 541. Reverse String II (0) | 2021.01.27 |