릿코드(LEETCODE)

1323. Maximum 69 Number

cepiloth 2020. 2. 3. 18:55
반응형

https://leetcode.com/problems/maximum-69-number/

 

Maximum 69 Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

준어진 숫자에서 6, 9의 숫자를 변경 하여 가장 큰 값을 찾는 문제

완전탐색으로 숫자 원소 순서마다 9 를 대입 하고 가장 큰 값을 찾으 면 된다.

 

class Solution {
public:
    int maximum69Number(int num) {

        string s = to_string(num);
        int sol = num;
        for (int i = 0; i < s.size(); i++) {
            string cand = s;
            cand[i] = '9';
            sol = max(sol, atoi(cand.c_str()));
        }

        return sol;
    }
};

 

 

반응형