[Programmers] #181847 - 0 떼기 [Java][C++][Python]
[Programmers] #181847 - 0 떼기 [Java][C++][Python]
1. 아이디어
정수로 이루어진 문자열 n_str에 대해 가장 왼쪽부터 연속으로 등장한 0을 뗀 문자열을 반환하는 간단한 문제다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(N)$ |
($N$ = n_str의 길이)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
class Solution {
public String solution(String n_str) {
for (int i = 0; i < n_str.length(); i++) {
if (n_str.charAt(i) != '0') return n_str.substring(i);
}
return null;
}
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;
string solution(string n_str) {
return n_str.substr(n_str.find_first_not_of('0'));
}
find_first_not_of 함수를 활용해 앞에서부터 연속된 0을 제거했다.
1
2
def solution(n_str):
return n_str.lstrip("0")
lstrip 함수를 활용해 앞에서부터 연속된 0을 제거했다.
This post is licensed under CC BY 4.0 by the author.