Skip to content

Latest commit

 

History

History
36 lines (30 loc) · 776 Bytes

Question_1323.md

File metadata and controls

36 lines (30 loc) · 776 Bytes

LeetCode Records - Question 1323 Maximum 69 Number

Attempt 1: Use an int[] to save the digits

class Solution {
    public int maximum69Number (int num) {
        int[] digits = new int[4];

        int count = 0;
        while (num > 0) {
            digits[count] = num % 10;
            count++;
            num /= 10;
        }

        for (int i = count - 1; i >= 0; i--) {
            if (digits[i] == 6) {
                digits[i] = 9;
                break;
            }
        }

        int result = 0;
        for (int i = count - 1; i >= 0; i--) {
            result *= 10;
            result += digits[i];
        }

        return result;
    }
}
  • Runtime: 0 ms (Beats: 100.00%)
  • Memory: 39.69 MB (Beats: 99.31%)