Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 1323. Maximum 69 Number

Rain Hu

1323. Maximum 69 Number


一、題目

You are given a positive integer num consisting only of digits 6 and 9.
Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).

Example 1:

Example 2:

Example 3:

Constraints:


二、分析

三、解題

1. Greedy

int maximum69Number (int num) {
    string s = to_string(num);
    for (int i = 0; i < s.length(); i++) {
        if (s[i] == '6') {
            s[i] = '9';
            break;
        }
    }
    return stoi(s);
}

回目錄 Catalog


Share this post on:

Previous
[LeetCode] 2460. Apply Operations to an Array
Next
[ML] 機器學習與統計學