aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-16 12:13:50 -0600
committerJack Sangdahl <[email protected]>2025-08-16 12:13:50 -0600
commitf359b85a14d62e61cd108f8c1f5818f08cde5a20 (patch)
treeeaf99b77a4503de6e8b88fa02c6cd1f3420a6d5c
parentf035e4f04939219d5979f67d9e36b307a60d15fb (diff)
downloadleetcode-f359b85a14d62e61cd108f8c1f5818f08cde5a20.tar.gz
leetcode-f359b85a14d62e61cd108f8c1f5818f08cde5a20.zip
1323. maximum 69 number
-rw-r--r--max_69_number.c29
-rw-r--r--max_69_number.py23
2 files changed, 52 insertions, 0 deletions
diff --git a/max_69_number.c b/max_69_number.c
new file mode 100644
index 0000000..40365b9
--- /dev/null
+++ b/max_69_number.c
@@ -0,0 +1,29 @@
+// 1323. Maximum 69 Number
+#include "leetcode.h"
+
+/*
+ * you are given a positive integer 'num' consisting only of digit 6 and 9.
+ * return the maximum number you can get by changing at most one digit. (6
+ * becomes 9, and 9 becomes 6).
+ */
+
+int maximum69Number(int num) {
+ int right = -1, cnt = 0, tmp = num;
+ while (tmp > 0) {
+ int digits = tmp % 10;
+ if (digits == 6)
+ right = cnt;
+ cnt++;
+ tmp /= 10;
+ }
+ if (right == -1)
+ return num;
+ int ans = num + (3 * pow(10, right));
+ return ans;
+}
+
+int main() {
+ printf("%d\n", maximum69Number(9669)); // expect: 9969
+ printf("%d\n", maximum69Number(9996)); // expect: 9999
+ printf("%d\n", maximum69Number(9999)); // expect: 9999
+}
diff --git a/max_69_number.py b/max_69_number.py
new file mode 100644
index 0000000..e8c843d
--- /dev/null
+++ b/max_69_number.py
@@ -0,0 +1,23 @@
+# 1323. Maximum 69 Number
+
+"""
+you are given a positive integer 'num' consisting only of digit 6 and 9.
+return the maximum number you can get by changing at most one digit. (6
+becomes 9, and 9 becomes 6).
+"""
+
+
+class Solution(object):
+ def maximum69Number(self, num):
+ """
+ :type num: int
+ :rtype: int
+ """
+ return int(str(num).replace("6", "9", 1))
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.maximum69Number(9669))
+ print(obj.maximum69Number(9996))
+ print(obj.maximum69Number(9999))