problemscpp
A collection of my answers to algorithm problems in c++.
lintcode.cpp
浏览该文件的文档.
1#include "lintcode.h"
2
3#include <algorithm>
4#include <climits>
5#include <cmath>
6#include <cstring>
7#include <map>
8#include <queue>
9#include <set>
10#include <sstream>
11#include <string>
12#include <unordered_set>
13
14using namespace std;
15
16namespace lintcode {
17 namespace license_key_formatting {
18 string Solution::licenseKeyFormatting(string &S, int K) {
19 auto S2 = ostringstream();
20 auto output = ostringstream();
21 int len = 0;
22 for(char c: S) {
23 if(isalnum(c) != 0) {
24 len++;
25 if(isdigit(c) != 0 || isupper(c) != 0) {
26 S2 << c;
27 } else if(islower(c) != 0) {
28 S2 << static_cast<char>(toupper(c));
29 }
30 }
31 }
32 string str = S2.str();
33 int first = len % K;
34 if(first == 0) {
35 first = K;
36 }
37 string str1 = str.substr(0, first);
38 string str2 = str.substr(first);
39 for(char c: str1) {
40 output << c;
41 }
42 int count = 0;
43 for(char c: str2) {
44 if(count == 0) {
45 output << '-';
46 }
47 output << c;
48 count++;
49 count %= K;
50 }
51 return output.str();
52 }
53 }// namespace license_key_formatting
54
55 namespace distribute_candies {
56 int Solution::distributeCandies(vector<int> &candies) {
57 auto m = map<long, int>();
58 auto ans = set<int>();
59 const int n = static_cast<int>(candies.size()) / 2;
60 int sum = 0;
61 for(const auto candy: candies) {
62 m[candy]++;
63 }
64 while(sum < n) {
65 for(auto candy: m) {
66 if(candy.second != 0) {
67 if(!ans.contains(static_cast<int>(candy.first))) {
68 ans.insert(static_cast<int>(candy.first));
69 if(ans.size() == m.size()) {
70 return static_cast<int>(ans.size());
71 }
72 }
73 candy.second--;
74 sum++;
75 }
76 if(sum >= n) {
77 break;
78 }
79 }
80 }
81 return static_cast<int>(ans.size());
82 }
83 }// namespace distribute_candies
84
85 namespace remove_extra {
86 string Solution::removeExtra(string &s) {
87 auto output = ostringstream();
88 bool start = true;
89 bool flag = false;
90 for(const char c: s) {
91 if(c != ' ') {
92 if(flag && !start) {
93 output << ' ';
94 }
95 output << c;
96 start = false;
97 flag = false;
98 } else {
99 flag = true;
100 }
101 }
102 return output.str();
103 }
104 }// namespace remove_extra
105
106 namespace character_deletion {
107 string Solution::CharacterDeletion(string &str, string &sub) {
108 auto oss = ostringstream();
109 auto us = unordered_set<char>();
110 for(auto ch: sub) {
111 us.insert(ch);
112 }
113 for(auto ch: str) {
114 if(!us.contains(ch)) {
115 oss << ch;
116 }
117 }
118 return oss.str();
119 }
120 }// namespace character_deletion
121
122 namespace judge_circle {
123 bool Solution::judgeCircle(string &moves) {
124 int x = 0;
125 int y = 0;
126 for(const char ch: moves) {
127 switch(ch) {
128 case 'R':
129 x--;
130 break;
131 case 'L':
132 x++;
133 break;
134 case 'U':
135 y--;
136 break;
137 case 'D':
138 y++;
139 break;
140 default: break;
141 }
142 }
143 return x == 0 && y == 0;
144 }
145 }// namespace judge_circle
146
147 namespace intersection {
148 vector<vector<int>> Solution::Intersection(vector<vector<int>> &a, vector<vector<int>> &b) {
149 vector<vector<int>> res;
150 if(a.empty() || b.empty()) {
151 return res;
152 }
153 for(int i = 0, j = 0; i < a.size() && j < b.size();) {
154 if(is_intersected(a[i], b[j])) {
155 res.emplace_back(vector({i, j}));
156 }
157 if(a[i][1] == b[j][1]) {
158 ++i;
159 ++j;
160 } else if(a[i][1] > b[j][1]) {
161 ++j;
162 } else {
163 ++i;
164 }
165 }
166 return res;
167 }
168
169 bool Solution::is_intersected(const vector<int> &l, const vector<int> &r) {
170 if(l[0] == r[0]) {
171 return true;
172 }
173 if(l[0] < r[0]) {
174 return r[0] <= l[1];
175 }
176 return l[0] <= r[1];
177 }
178 }// namespace intersection
179
180 namespace flatten {
182 if(root == nullptr) {
183 return;
184 }
185 root = vlr(root);
186 }
187
189 if(node->left != nullptr) {
190 if(node->right != nullptr) {
191 auto *tmp = node->right;
192 node->right = vlr(node->left);
193 auto *current = node->right;
194 while(current->right != nullptr) {
195 current = current->right;
196 }
197 current->right = vlr(tmp);
198 node->left = nullptr;
199 } else {
200 node->right = vlr(node->left);
201 node->left = nullptr;
202 }
203 } else {
204 if(node->right != nullptr) {
205 node->right = vlr(node->right);
206 }
207 }
208 return node;
209 }
210 }// namespace flatten
211
212 namespace convert {
213 string Solution::convert(long long index) {
214 unsigned long long prefix = index / 702 + 1;
215 if(index % 702 == 0) {
216 prefix--;
217 index = 702;
218 } else {
219 index %= 702;
220 }
221 auto ans = string();
222 bool round = false;
223 while(index != 0) {
224 char ch = 0;
225 if(round) {
226 ch = static_cast<char>(index % 26 + 63);
227 round = false;
228 } else {
229 ch = static_cast<char>(index % 26 + 64);
230 }
231 if(ch == '@' && index >= 26) {
232 ch = 'Z';
233 round = true;
234 } else if(ch == '?' && index >= 26) {
235 ch = 'Y';
236 round = true;
237 }
238 if('A' <= ch && ch <= 'Z') {
239 ans.insert(0, 1, ch);
240 }
241 index /= 26;
242 }
243 return to_string(prefix) + ans;
244 }
245 }// namespace convert
246
247 namespace min_path_sum {
248 int Solution::minPathSum(vector<vector<int>> &grid) {
249 const unsigned int m = grid.size();
250 const unsigned int n = grid[0].size();
251 auto *dp = new int *[m + 1];
252 for(int i = 0; i <= m; i++) {
253 dp[i] = new int[n + 1];
254 memset(dp[i], INT_MAX, (n + 1) * sizeof(int));
255 }
256 dp[1][1] = grid[0][0];
257 for(int i = 1; i <= m; i++) {
258 for(int j = 1; j <= n; j++) {
259 if(i == 1 && j == 1) {
260 continue;
261 }
262 unsigned int min = dp[i - 1][j];
263 if(dp[i][j - 1] < min) {
264 min = dp[i][j - 1];
265 }
266 dp[i][j] = grid[i - 1][j - 1] + min;
267 }
268 }
269 const auto ans = dp[m][n];
270 for(int i = 0; i <= m; i++) {
271 delete[] dp[i];
272 }
273 delete[] dp;
274 return ans;
275 }
276 }// namespace min_path_sum
277
278 namespace digit_counts {
279 int Solution::digitCounts(int k, int n) {
280 int count = 0;
281 for(int i = 0; i <= n; i++) {
282 int cpy = i;
283 while(cpy != 0) {
284 if(cpy % 10 == k) {
285 count++;
286 }
287 cpy /= 10;
288 }
289 }
290 if(k == 0) {
291 count++;
292 }
293 return count;
294 }
295 }// namespace digit_counts
296
297 namespace min_diff_in_BST {
299 auto vals = vector<int>();
300 auto que = queue<TreeNode *>();
301 que.push(root);
302 while(!que.empty()) {
303 const auto *node = que.front();
304 que.pop();
305 vals.push_back(node->val);
306 if(node->left != nullptr) {
307 que.push(node->left);
308 }
309 if(node->right != nullptr) {
310 que.push(node->right);
311 }
312 }
313 sort(vals.begin(), vals.end());
314 int minimum = INT_MAX;
315 for(int i = 0; i + 1 < vals.size(); i++) {
316 minimum = min(vals[i + 1] - vals[i], minimum);
317 }
318 return minimum;
319 }
320 }// namespace min_diff_in_BST
321}// namespace lintcode
vector< int > root
Definition: acwing408.cpp:349
TreeNode * right
Definition: lintcode.h:13
TreeNode * left
Definition: lintcode.h:13
static string licenseKeyFormatting(string &, int)
Definition: lintcode.cpp:18
static int distributeCandies(vector< int > &candies)
Definition: lintcode.cpp:56
static string removeExtra(string &s)
Definition: lintcode.cpp:86
static string CharacterDeletion(string &str, string &sub)
Definition: lintcode.cpp:107
static bool judgeCircle(string &moves)
Definition: lintcode.cpp:123
static vector< vector< int > > Intersection(vector< vector< int > > &a, vector< vector< int > > &b)
Definition: lintcode.cpp:148
static bool is_intersected(const vector< int > &l, const vector< int > &r)
Definition: lintcode.cpp:169
static TreeNode * vlr(TreeNode *)
Definition: lintcode.cpp:188
static void flatten(TreeNode *root)
Definition: lintcode.cpp:181
static string convert(long long index)
Definition: lintcode.cpp:213
static int minPathSum(vector< vector< int > > &grid)
Definition: lintcode.cpp:248
static int digitCounts(int k, int n)
Definition: lintcode.cpp:279
static int minDiffInBST(TreeNode *root)
Definition: lintcode.cpp:298