반응형
https://leetcode.com/problems/reverse-prefix-of-word
ch의 index를 찾아준 뒤 뒤집어줍니다.
class Solution {
public:
string reversePrefix(string word, char ch) {
int idx = find(word, ch);
if(idx == -1) return word;
for(int i=0; i<=idx/2; i++) swap(word[i], word[idx-i]);
return word;
}
int find(string& str, char ch) {
for(int i=0; i<str.size(); i++) {
if(str[i] == ch) return i;
}
return -1;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 165. Compare Version Numbers (0) | 2024.05.03 |
---|---|
LeetCode 2441. Largest Positive Integer That Exists With Its Negative (0) | 2024.05.03 |
LeetCode 2997. Minimum Number of Operations to Make Array XOR Equal to K (0) | 2024.04.29 |
LeetCode 752. Open the Lock (0) | 2024.04.29 |
LeetCode 310. Minimum Height Trees (0) | 2024.04.23 |