[Algorithm] 최댓값과 최솟값

2025. 1. 22. 09:35·Algorithm/Practice

문제 설명

문자열 s에는 공백으로 구분된 숫자들이 저장되어 있습니다. str에 나타나는 숫자 중 최소값과 최대값을 찾아 이를 "(최소값) (최대값)"형태의 문자열을 반환하는 함수, solution을 완성하세요. 예를들어 s가 "1 2 3 4"라면 "1 4"를 리턴하고, "-1 -2 -3 -4"라면 "-4 -1"을 리턴하면 됩니다.

 

문제 풀이

  1. 공백 문자를 구분한다.
    • stringstream : 공백 또는 '\n' 까지 문자열을 확인하고 반환한다. 
  2. 문자열을 숫자로 치환
    • stringstream >> 연산자를 사용한다.
  3. 숫자를 문자열로 치환 
    • to_string()

 

코드 작성

#include <string>
#include <sstream>
#include <climits>

using namespace std;

string solution(string s) {
    int minValue = INT_MAX;
    int maxValue = INT_MIN;
    
    int token;
    stringstream stream(s);
    while (stream >> token)
    {
        if (minValue > token)
            minValue = token;
        if (maxValue < token)
            maxValue = token;
    }
    
    return to_string(minValue) + " " + to_string(maxValue);
}

'Algorithm > Practice' 카테고리의 다른 글

[Algorithm] 이진 변환 반복하기  (0) 2025.01.22
[Algorithm] JadenCase 문자열 만들기  (1) 2025.01.22
[Algorithm] 신고 결과 받기  (0) 2025.01.21
[Algorithm] 공원 산책  (1) 2025.01.20
[Algorithm] 개인정보 수집 유효기간  (0) 2025.01.17
'Algorithm/Practice' 카테고리의 다른 글
  • [Algorithm] 이진 변환 반복하기
  • [Algorithm] JadenCase 문자열 만들기
  • [Algorithm] 신고 결과 받기
  • [Algorithm] 공원 산책
DevColIn
DevColIn
복잡함을 단순하게
  • DevColIn
    심플한 코딩생활
    복잡함을 단순하게
  • 전체
    오늘
    어제
    • 전체보기 (223)
      • Unreal 부트캠프 (49)
        • TIL (34)
        • 사전캠프 (7)
        • 본캠프 (8)
      • Unrael (10)
        • 환경설정 (0)
        • Basic (19)
        • Component (5)
        • GAS (GameplayAbilitySystem) (3)
        • AI (2)
        • Implement (10)
        • UI (1)
        • Error (1)
        • Network (2)
        • Tip (1)
      • Level Design (5)
      • Math (1)
      • Design Pattern (16)
      • Computer Science (2)
        • Network (1)
        • Database (1)
      • Algorithm (79)
        • Basic (4)
        • Practice (74)
      • C++ (4)
        • Basic (4)
      • Tool (0)
      • Game (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    사전캠프
    본캠프
    component
    하드 레퍼런싱
    액터
    assetmanager
    디자인패턴
    unrealengine
    basic
    unreal
    알고리즘
    AI
    Implement
    Til
    tsoftobjectptr
    소프트 레퍼런신
    gas
    내일배움캠프
    DesignPattern
    레벨디자인
    GameplayEffect
    KPT회고
    Algorithm
    c++
    Design Pattern
    퀘스트
    Animation
    게임동기화
    actor
    디자인 패턴
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
DevColIn
[Algorithm] 최댓값과 최솟값
상단으로

티스토리툴바