반응형
숫자와 .으로 구성된 문자열이 주어졌을 때
. 앞뒤로 []를 넣어주는 문제.
주어진 문자열 처음부터 보면서
숫자면 그냥 추가하고
. 을 만나면 앞뒤로 대괄호를 넣어주면 된다.
class Solution {
public:
string defangIPaddr(string address) {
string ans;
for(int i = 0;i<address.size();i++){
if(address[i] =='.'){
ans+= '[';
ans+= '.';
ans+= ']';
}
else
ans+=address[i];
}
return ans;
}
};
반응형
'IT > 알고리즘' 카테고리의 다른 글
6] Leetcode 771. Jewels and Stones (0) | 2020.06.21 |
---|---|
5] Leetcode 1342. Number of Steps to Reduce a Number to Zero (0) | 2020.06.21 |
3] Leetcode 1431. Kids With the Greatest Number of Candies (0) | 2020.06.18 |
2] Leetcode 1470. Shuffle the Array (0) | 2020.06.16 |
1] Leetcode 1480. Running Sum of 1d Array (0) | 2020.06.16 |
댓글