-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum_swap.cpp
More file actions
57 lines (52 loc) · 1.33 KB
/
maximum_swap.cpp
File metadata and controls
57 lines (52 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* =====================================================================================
*
* Filename: maximum_swap.cpp
*
* Description: 670. Maximum Swap. https://leetcode.com/problems/maximum-swap/
*
* Version: 1.0
* Created: 12/31/2023 23:37:29
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <algorithm>
#include <string>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::string;
using std::vector;
class Solution {
public:
int maximumSwap(int num) {
string digits = std::to_string(num);
int buckets[10] = {0};
for (int i = 0; i < digits.length(); i++) {
buckets[digits[i] - '0'] = i;
}
for (int i = 0; i < digits.length(); i++) {
for (int j = 9; j > digits[i] - '0'; j--) {
if (buckets[j] > i) {
std::swap(digits[i], digits[buckets[j]]);
return std::stoi(digits);
}
}
}
return num;
}
};
TEST(Solution, calculate) {
vector<std::pair<int, int>> cases = {
std::make_pair(2736, 7236),
std::make_pair(9973, 9973),
};
for (const auto& c : cases) {
EXPECT_EQ(Solution().maximumSwap(c.first), c.second);
}
}