-
Notifications
You must be signed in to change notification settings - Fork 1
/
fun.cpp
334 lines (272 loc) · 6.11 KB
/
fun.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <iostream>
#include <limits>
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <vector>
#include <sys/mman.h>
#include <unordered_map>
#include <string>
#include <iterator>
#include <thread>
#include <queue>
#include <algorithm>
#include <unordered_set>
using namespace std;
std::pair<int,int> getEraseBounds(string str){
int left = 0, right = 0;
while (str[right++] != '1');
pair<int,int> p;
p = make_pair(left, right - 1);
return p;
}
template <typename T>
void printBinary(T a){
int size = sizeof(a) * 8;
int cnt = 0;
string s = "";
while (cnt < size){
s.insert(0, std::to_string((a >> cnt) & 0x1));
cnt++;
}
std::pair<int,int> erasePair = getEraseBounds(s);
if (right > 0)
s.erase(s.begin() + erasePair.first, s.begin() + erasePair.second);
string prefix = "0x";
s.insert(0, prefix);
cout << s << endl;
}
/* play with string
char a[] = "hello";
int b[] = {1, 2, 3, 4, 5};
printf("a: %lu\n", sizeof(a));
printf("b memory size: %lu bytes\n", sizeof(b));
printf("b elements: %lu\n", sizeof(b)/sizeof(int));
char *aaa = (char*)malloc(sizeof(char)*5);
aaa = "sgood";
char *aa = "good";
char bb[] = "good";
printf("%p\n", aaa);
printf("%p\n", aa);
printf("%pp\n", bb);
printBinary(aaa[0]);
printf("\n%d\n", aaa[0]);
printBinary(aaa[0]);
*/
void getLen(int &cnt, char *s){
while ( *s != '\0'){
s++;
cnt++;
}
}
int myAtoi(string str) {
// check valid;
for(int i = 0; i < str.length(); i++) if (str[i] - '0' > 9) return -1;
int len = str.length();
int res = 0;
int i = 0;
while (len != 0){
res += (str[i] - '0') * (len * 10);
i++, len--;
}
return res;
}
auto functor = [](string s1, string s2) -> bool {
int i = 0;
while ( i != s1.length() || i != s2.length() ){
if (s1[i] - s2[i]) return true;
else return false;
}
return true;
};
vector<vector<string>> groupAnagrams(vector<string> &list){
vector<vector<string>> result;
unordered_map<string, vector<string>> map;
int i = 0;
for(string s : list ){
sort(s.begin(), s.end());
map[s].push_back( list[i++] );
}
for(auto p : map){
sort(p.second.begin(), p.second.end());
//cout << p.second << "\t";
result.push_back(p.second);
}
for(vector<string> list : result) {
for(string s : list){
cout << s << "\t";
}
cout << endl;
}
return result;
}
/*
int main(){
vector<int> nums = { 2, 7, 11, 15, 11};
unordered_map<int, int> map;
for_each(nums.begin(), nums.end(), [&map](int num)
{
if (map.find(num) == map.end()){
map[num] = 1;
} else if (map.find(num) != map.end())
{
map[num]++;
}
}
);
for(auto p : map){
cout << p.first << " " << p.second << endl;
}
return 0;
}
*/
int BitCount(int n ){
unsigned int c = 0;
for( c= 0; n; n >>= 1) {
c += n & 0x1;
}
return c;
}
template <class T>
class A {
T a;
public:
void print(){ cout << "hello"; }
};
template <class T>
class B : public A<T> {
public:
void print(){ cout << "hello"; }
};
template <class T>
void oddNumber(vector<T> v){
unordered_set<T> set;
for(int i = 0; i < v.size(); i++) {
if ( !set.insert(v[i]).second ) {
set.erase(v[i]);
}
}
for_each( set.begin(), set.end(), [](T elem)
{ cout << elem << " "; } );
return;
}
template <typename Iterator>
void reverse(Iterator &begin, Iterator &end){
if (begin == end) return;
typename::iterator_traits<Iterator>::value_type temp = *begin;
*begin = *end;
*end = temp;
reverse( begin + 1, end - 1);
}
struct Node {
int data;
Node *next;
Node(int _data): data(_data) {}
};
class LinkedList{
int size;
Node *head;
public:
LinkedList():size(0), head(nullptr){}
~LinkedList(){
while (head -> next != nullptr) {
Node *node = head;
head = head -> next;
delete node;
}
}
void push(int elem){
Node* node = head;
if (node == nullptr) { node = new Node(elem); return; }
while (node -> next != nullptr) {
node = node -> next;
}
node -> next = new Node(elem);
}
void traverse(){
Node* node = head;
while (node != nullptr) {
cout << node -> data << "\t";
node = node -> next;
}
}
};
int max(int n){
if (n == 2)
return 1;
else if (n == 3)
return 2;
else if (n % 3 == 0)
return (int)pow(3, n/3);
else if (n % 3 == 1)
return 2 * 2 * (int)pow(3, (n - 4)/ 3);
else
return 2 * (int)pow(3, (n - 2) /3);
}
struct Purchase
{
string itemId;
int quantity;
Purchase(string item, int quan)
: itemId(item), quantity(quan)
{}
};
vector<string> find(vector<Purchase> list, int threshold)
{
vector<string> res;
for( const Purchase pc : list )
if ( pc.quantity > threshold ) res.push_back( pc.itemId );
return res;
}
ostream & operator << (ostream &os, vector<Purchase> pc)
{
for( Purchase s : pc)
os << s.itemId << " ";
os << endl;
return os;
}
ostream & operator << (ostream &os, vector<string> res)
{
for( string s : res)
os << s << " ";
os << endl;
return os;
}
class Pool // global unique
{
private:
static Pool *instance;
Pool(){ value = 1; } // user cannot instantiate a class by himself
int value;
public:
static Pool *getInstance()
{
if (instance == nullptr)
instance = new Pool;
return instance;
}
void print()
{
cout << "value :" << value << endl;
}
void setValue( int n)
{
value = n;
}
Pool(Pool &rhs) = delete;
Pool& operator = (Pool const &rhs) = delete;
};
Pool *Pool::instance = 0; // avoid using this pattern most of cases
int majorityElement(vector<int> num) {
int result=num[0];int len = num.size();
int count = 0;
for (int i=0;i<len;i++){
if (count==0||result==num[i]) {
result = num[i];count++;} //count清零时,取当前数作为result
else count--;
}
return result;
}
int main()
{
}