-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode.cpp
More file actions
325 lines (315 loc) · 6.03 KB
/
Copy pathleetcode.cpp
File metadata and controls
325 lines (315 loc) · 6.03 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
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
#include<iostream>
#include<vector>
using namespace std;
//que no.13
/*
class Solution {
public:
int romanToInt(string s) {
int sum =0,count=0 ;
for(int i=0;s[i]!='\0'; ++i)
{
switch (s[i])
{
case 'I':
if(s[i+1]=='V')
{sum+=4;
i++;}
else if (s[i+1]=='X')
{sum+=9;i++;
}
else
sum+=1;
break;
case 'V':
sum+=5;
break;
case 'X':
if(s[i+1]=='L')
{
sum+=40;
i++;
}
else if(s[i+1]=='C')
{
sum+=90;
i++;
}
else
sum+=10;
break;
case 'L':
sum+=50;
break;
case 'C':
if(s[i+1]=='D')
{
sum+=400;
i++;
}
else if(s[i+1]=='M')
{
sum+=900;
i++;
}
else
sum+=100;
break;
case 'D':
sum+=500;
break;
case 'M':
sum+=1000;
break ;
}
}
return sum ;
}
};
int main()
{
class Solution o; string star;
cin>>star;
cout<<o.romanToInt(star);
return 0 ;
}
*/
//
//que no.20 my method
/*
class Solution {
public:
bool isValid(string s) {
char start[3]={'(','{','['};
char end[3]={')','}',']'};
int count[3]={0},a=1;
for(int i=0;s[i]!='\0';++i)
{
for(int j=0;j<3;++j)
{
if(s[i]==start[j])
count[j]+=a;
else if(s[i]==end[j])
count[j]=count[j]-a;
if(count[j]<0)
return false;
if(count[j]==0)
a++;
}
}
int result=0;
for(int i=0;i<3;++i)
if(count[i]==0)
result++;
if(result==3)
return true;
else
return false;
}
}
//gpt method using concept of stacking
class Solution {
public:
bool isValid(string s) {
char arr[10000];
int top =0 ;
for(int c:s)
{
if( c=='(' || c=='[' || c=='{' )
arr[top++]=c;
else
{
if(top==0) return false ;
char last = arr[top-1];
if((c == ')' && last == '(')||(c=='}'&&last=='{')||(c==']'&&last=='[') )
top--;
else
return false;
}
}
return top==0;
}
};
*/
#include<unordered_map>
// que. no.1 two sum , using gpt
/*
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> sum;
int i=0;
for (int i=0;i<nums.size();++i)
{
int need = target - nums[i];
if(sum.find(need)!= sum.end())
return {sum[need],i};
else sum[need]=i;
}
return {};
}
};
*/
// Que no.217 contain duplicate
/*
// 1st try
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map<int,int> check;
for(int c:nums)
{
check[c]++;
if(check[c]>1)
return true ;
}
return false ;
}
};
\\ 2nd try
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set <int> check;
int count=0 ;
for(int c:nums)
{
check.insert(c);
count++;
if(check.size()!=count)
return true ;
}
return false ;
}
};
\\ 3rd try
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set <int> check;
for(int i=0;i<nums.size();++i)
{
check.insert(nums[i]);
if(check.size()!=i+1)
return true ;
}
return false ;
}
};
\\ Chatgpt
class Solution {
public:
bool containsDuplicate(const vector<int>& nums) {
unordered_set<int> seen;
seen.reserve(nums.size()); // avoid rehashing for big inputs
for (int x : nums) {
auto p = seen.insert(x); // insert returns pair<iterator,bool>
if (!p.second) return true; // if already present, insert fails
}
return false;
}
};
*/
// que no.242 Valid anagram
/*
// fastest in runtime 0ms....
class Solution {
public:
bool isAnagram(string s, string t) {
unordered_map<char,int> ana;
int i;
if(s.size()!=t.size())
return false ;
for(i=0 ; s[i]!='\0';++i )
{
ana[s[i]]++;
}
i=0;
while(t[i]!='\0')
{
ana[t[i]]--;
if(ana[t[i]]<0)
return false ;
++i;
}
return true;
}
};
// most direct and shortest code
class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(),s.end());
sort(t.begin(),t.end());
if(s==t)
return true;
else
return false ;
}
};
*/
// Que no.125 Valid Palindrome
/*
class Solution {
public:
bool isPalindrome(string s) {
vector<char>check;int a=0;
int i ;
for(i =0 ; s[i]!='\0';++i)
{
if(s[i]>='a'&&s[i]<='z')
check.push_back(s[i]);
if(s[i]>='A'&&s[i]<='Z')
check.push_back(s[i]+32);
}
int c = check.size();
for(int j=0;j<c/2;++j)
{
if(check[j]!=check[c-j-1])
return false ;
}
return true;
}
};
*/
class Solution()
{
public:
int isPalindrome();
};
int main()
{
class Solution o;
string s ;
cin>>s;
bool tell=o.isPalindrome(s);
cout<<tell<<"hello";
//How to do my name , ok i guess it works ok m
}
class Solution {
public:
int reverse(int x) {
int i,rev=0,place=1,check=0 ;
if(x>=pow(2,31)-1||x<=-pow(2,31))
return 0;
if(x*(-1)>0&&x>-pow(2,31))
{
x*=(-1);
check=1;
}
if(x>0&&x<pow(2,31)-1)
for(i=0;x>0 ;++i )
{
if(x>0&&rev>pow(2,31)/10)
return 0;
int b = x % 10;
rev=rev*10+b;
x=x/10;
}
else
return 0 ;
if(check==1)
rev*=(-1);
return rev;
}
};