-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum_odd_number.cpp
56 lines (51 loc) · 1.21 KB
/
maximum_odd_number.cpp
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
/*
* =====================================================================================
*
* Filename: maximum_odd_number.cpp
*
* Description: 2864. Maximum Odd Binary Number
*
* Version: 1.0
* Created: 07/21/2024 15:59:30
* Revision: none
* Compiler: gcc
*
* Author: [email protected]
* Organization:
*
* =====================================================================================
*/
#include <string>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::pair;
using std::string;
using std::vector;
class Solution {
public:
string maximumOddBinaryNumber(string s) {
string odd(s.size() + 1, '0');
int l = 0;
int r = s.size();
for (const char c : s) {
if (c == '1') {
odd[l++] = c;
} else {
odd[r--] = c;
}
}
std::swap(odd[0], odd[s.size()]);
return odd.substr(1);
}
};
TEST(Solution, maximumOddBinaryNumber) {
vector<pair<string, string>> cases = {
std::make_pair("010", "001"),
std::make_pair("0101", "1001"),
};
for (auto& s : cases) {
EXPECT_EQ(Solution().maximumOddBinaryNumber(s.first), s.second);
}
}