2231. Largest Number After Digit Swaps by Parity Easy

@problem@discussion
#Sorting#Heap (Priority Queue)



1/**
2 * [2231] Largest Number After Digit Swaps by Parity
3 *
4 * You are given a positive integer num. You may swap any two digits of num that have the same parity (i.e. both odd digits or both even digits).
5 * Return the largest possible value of num after any number of swaps.
6 *  
7 * Example 1:
8 * 
9 * Input: num = 1234
10 * Output: 3412
11 * Explanation: Swap the digit 3 with the digit 1, this results in the number 3214.
12 * Swap the digit 2 with the digit 4, this results in the number 3412.
13 * Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.
14 * Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.
15 * 
16 * Example 2:
17 * 
18 * Input: num = 65875
19 * Output: 87655
20 * Explanation: Swap the digit 8 with the digit 6, this results in the number 85675.
21 * Swap the first digit 5 with the digit 7, this results in the number 87655.
22 * Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.
23 * 
24 *  
25 * Constraints:
26 * 
27 * 	1 <= num <= 10^9
28 * 
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity/
33// discuss: https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38    pub fn largest_integer(num: i32) -> i32 {
39        0
40    }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_2231() {
51    }
52}
53


Back
© 2025 bowen.ge All Rights Reserved.