문제설명
정수로 이루어진 문자열 n_str이 주어질 때, n_str의 가장 왼쪽에 처음으로 등장하는 0들을 뗀 문자열을 return하도록 solution 함수를 완성해주세요.
제한사항
- 2 ≤ n_str ≤ 10
- n_str이 "0"으로만 이루어진 경우는 없습니다.
입출력 예
입출력 예 설명
입출력 예 #1
- "0010"의 가장 왼쪽에 연속으로 등장하는 "0"을 모두 제거하면 "10"이 됩니다.
입출력 예 #2
- "854020"는 가장 왼쪽에 0이 없으므로 "854020"을 return합니다.
작성코드
#include <string>
#include <vector>
using namespace std;
string solution(string n_str) {
// Find the first non-zero character index
size_t firstNonZero = n_str.find_first_not_of('0');
// If there are no non-zero characters, return "0"
if (firstNonZero == string::npos) {
return "0";
}
// Otherwise, return the substring starting from the first non-zero character
return n_str.substr(firstNonZero);
}
문제 URL
https://school.programmers.co.kr/learn/courses/30/lessons/181847
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
'C++ > 코딩 기초 트레이닝' 카테고리의 다른 글
[C++/프로그래머스] 문자열로 변환 (0) | 2023.12.15 |
---|---|
[C++/프로그래머스] 두 수의 합 (0) | 2023.12.15 |
[C++/프로그래머스] 문자열을 정수로 변환하기 (0) | 2023.12.14 |
[C++/프로그래머스] 문자열 정수의 합 (0) | 2023.12.14 |
[C++/프로그래머스] 정수 부분 (0) | 2023.12.14 |