반응형

https://leetcode.com/problems/largest-3-same-digit-number-in-string/description/

 

Largest 3-Same-Digit Number in String - LeetCode

Can you solve this real interview question? Largest 3-Same-Digit Number in String - You are given a string num representing a large integer. An integer is good if it meets the following conditions: * It is a substring of num with length 3. * It consists of

leetcode.com

 

연속된 세 개의 숫자가 같다면, 해당 수에 대해 최댓값을 업데이트해줍니다.

class Solution {
public:
    string largestGoodInteger(string num) {
        int mx = -1;
        for(int i=2; i<num.size(); i++) {
            if(num[i] == num[i - 1] && num[i] == num[i - 2]) mx = max(mx, num[i] - '0');
        }

        if (mx == -1) return "";
        else if(mx == 0) return "000";
        return to_string(mx * 100 + mx * 10 + mx);
    }
};

 

반응형

+ Recent posts