-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderly_queue.cpp
66 lines (62 loc) · 1.41 KB
/
orderly_queue.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
57
58
59
60
61
62
63
64
65
66
// =====================================================================================
//
// Filename: orderly_queue.cpp
//
// Description: 899. Orderly Queue.
//
// Version: 1.0
// Created: 08/15/2019 11:49:00 AM
// Revision: none
// Compiler: g++
//
// Author: Zhu Xianfeng (), [email protected]
// Organization:
//
// =====================================================================================
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
using std::string;
class Solution
{
public:
string orderlyQueue(const string& s, int k)
{
if (k < 1)
{
return s;
}
else if (k == 1)
{
string src = s + s;
string dst = s;
for (int i = 1; i < s.size(); i++)
{
auto sub = src.substr(i, s.size());
if (sub < dst)
{
dst = sub;
}
}
return dst;
}
// k > 1, sort string
string d = s;
std::sort(d.begin(), d.end());
return d;
}
};
int main(int argc, char* argv[])
{
string s = "cba";
int k = 1;
if (argc > 2)
{
s = argv[1];
k = atoi(argv[2]);
}
auto d = Solution().orderlyQueue(s, k);
printf("%s, %d -> %s\n", s.c_str(), k, d.c_str());
return 0;
}