Skip to content

Commit 6a3a245

Browse files
ME-ON1actions-user
authored andcommitted
Adding new LC solutions
1 parent 23031f8 commit 6a3a245

File tree

219 files changed

+6868
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

219 files changed

+6868
-0
lines changed

412268702_two-sum.cxx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
id = 412268702
2+
lang = cpp
3+
runtime = 24 ms
4+
memory = 10.9 MB
5+
title_slug = two-sum
6+
code =
7+
```class Solution {
8+
public:
9+
vector<int> twoSum(vector<int>& nums, int target) {
10+
vector<int> v ;
11+
map<int,int> mp ;
12+
for(int i = 0 ; i < nums.size(); i++ ){
13+
int j ;
14+
j = nums[i];
15+
pair<int, int > p ;
16+
p = make_pair(j, i );
17+
mp.insert(p);
18+
}
19+
20+
for(int i = 0 ; i < nums.size(); i++ ){
21+
int difw = target - nums[i] ;
22+
if(mp.find(difw) != mp.end() && mp[difw] != i){
23+
v.push_back(mp[difw]);
24+
v.push_back(i);
25+
return v;
26+
}
27+
}
28+
return v ;
29+
}
30+
};```

449428514_add-two-numbers.cxx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
id = 449428514
2+
lang = cpp
3+
runtime = 20 ms
4+
memory = 71.2 MB
5+
title_slug = add-two-numbers
6+
code =
7+
```/**
8+
* Definition for singly-linked list.
9+
* struct ListNode {
10+
* int val;
11+
* ListNode *next;
12+
* ListNode() : val(0), next(nullptr) {}
13+
* ListNode(int x) : val(x), next(nullptr) {}
14+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
15+
* };
16+
*/
17+
class Solution {
18+
public:
19+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
20+
ListNode lk;
21+
ListNode* tmp = &lk ;
22+
int carry = 0 ;
23+
while(l1 || l2){
24+
int data = (l1 ? l1->val : 0) + (l2 ? l2->val: 0) + carry ;
25+
carry = data/10;
26+
tmp->next =new ListNode(data%10);
27+
tmp = tmp->next ;
28+
if(l1) l1 = l1->next ;
29+
if(l2) l2 = l2->next ;
30+
}
31+
if(carry){
32+
tmp->next = new ListNode(1) ;
33+
}
34+
35+
return lk.next ;
36+
}
37+
};```

449756869_reverse-integer.cxx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
id = 449756869
2+
lang = cpp
3+
runtime = 0 ms
4+
memory = 5.9 MB
5+
title_slug = reverse-integer
6+
code =
7+
```class Solution {
8+
public:
9+
int reverse(int x) {
10+
int j = x;
11+
int rev = 0 ;
12+
while(x != 0 ) {
13+
int rem = x %10 ;
14+
x /= 10 ;
15+
if(rev > INT_MAX/10 || (rev == INT_MAX/10 && rem > 7)){
16+
return 0 ;
17+
}
18+
if(rev < INT_MIN/10 || (rev == INT_MAX/10 && rev < -8)){
19+
return 0;
20+
}
21+
rev = rem + rev*10 ;
22+
}
23+
return rev ;
24+
}
25+
};```

467529137_palindrome-number.cxx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
id = 467529137
2+
lang = cpp
3+
runtime = 12 ms
4+
memory = 6 MB
5+
title_slug = palindrome-number
6+
code =
7+
```class Solution {
8+
public:
9+
bool isPalindrome(int x) {
10+
if(x < 0) {
11+
return false ;
12+
}
13+
//count the length of x
14+
int div = 1 ;
15+
while(x / div >= 10) {
16+
div *= 10 ;
17+
18+
}
19+
20+
while(x != 0 ) {
21+
int lead = x / div ;
22+
int trail = x % 10 ;
23+
24+
// cout <<lead << " " << trail << "\n " ;
25+
if(lead != trail ) {
26+
return false ;
27+
}
28+
29+
x = (x % div ) / 10 ;
30+
div /= 100 ;
31+
// cout << x << " x \n" ;
32+
}
33+
34+
return true ;
35+
36+
}
37+
};```

467576341_palindrome-linked-list.cxx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
id = 467576341
2+
lang = cpp
3+
runtime = 224 ms
4+
memory = 121 MB
5+
title_slug = palindrome-linked-list
6+
code =
7+
```/**
8+
* Definition for singly-linked list.
9+
* struct ListNode {
10+
* int val;
11+
* ListNode *next;
12+
* ListNode() : val(0), next(nullptr) {}
13+
* ListNode(int x) : val(x), next(nullptr) {}
14+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
15+
* };
16+
*/
17+
class Solution {
18+
public:
19+
20+
bool checkPallin(ListNode * tail , ListNode* &head ) {
21+
if(!tail ) {
22+
return true ;
23+
}
24+
bool che = checkPallin(tail->next , head ) /*&& tail->val == head->val */;
25+
if(head->val == tail->val) {
26+
che = che && true;
27+
}else {
28+
che = che && false ;
29+
}
30+
head = head->next ;
31+
return che ;
32+
}
33+
34+
bool isPalindrome(ListNode* head) {
35+
if(head == NULL || head->next == NULL) {
36+
return true ;
37+
}
38+
ListNode * tail = head ;
39+
// cout << checkPallin(tail, head) << " sdf\n" ;
40+
return checkPallin(tail , head) ;
41+
42+
}
43+
};```

467576574_palindrome-linked-list.cxx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
id = 467576574
2+
lang = cpp
3+
runtime = 204 ms
4+
memory = 121.1 MB
5+
title_slug = palindrome-linked-list
6+
code =
7+
```/**
8+
* Definition for singly-linked list.
9+
* struct ListNode {
10+
* int val;
11+
* ListNode *next;
12+
* ListNode() : val(0), next(nullptr) {}
13+
* ListNode(int x) : val(x), next(nullptr) {}
14+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
15+
* };
16+
*/
17+
class Solution {
18+
public:
19+
20+
bool checkPallin(ListNode * tail , ListNode* &head ) {
21+
if(!tail ) {
22+
return true ;
23+
}
24+
bool che = checkPallin(tail->next , head ) && tail->val == head->val ;
25+
head = head->next ;
26+
return che ;
27+
}
28+
29+
bool isPalindrome(ListNode* head) {
30+
if(head == NULL || head->next == NULL) {
31+
return true ;
32+
}
33+
ListNode * tail = head ;
34+
// cout << checkPallin(tail, head) << " sdf\n" ;
35+
return checkPallin(tail , head) ;
36+
37+
}
38+
};```

467970028_roman-to-integer.cxx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
id = 467970028
2+
lang = cpp
3+
runtime = 8 ms
4+
memory = 8.1 MB
5+
title_slug = roman-to-integer
6+
code =
7+
```class Solution {
8+
public:
9+
int romanToInt(string s) {
10+
int num = 0;
11+
unordered_map<char,int> mp ;
12+
13+
mp['I'] = 1 ;
14+
mp['V'] = 5 ;
15+
mp['X'] = 10 ;
16+
mp['L'] = 50 ;
17+
mp['C'] = 100 ;
18+
mp['D'] = 500 ;
19+
mp['M'] = 1000 ;
20+
num = mp[s[s.size() -1 ]] ;
21+
for(int i = s.size() - 2 ; i >= 0; i-- ) {
22+
char cur = s[i] ;
23+
char prev = s[i+1] ;
24+
25+
if(mp[cur] >= mp[prev]) {
26+
num += mp[cur] ;
27+
}else {
28+
num -= mp[cur] ;
29+
}
30+
}
31+
return abs(num) ;
32+
33+
}
34+
35+
};```

467991088_longest-common-prefix.cxx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
id = 467991088
2+
lang = cpp
3+
runtime = 0 ms
4+
memory = 9.3 MB
5+
title_slug = longest-common-prefix
6+
code =
7+
```class Solution {
8+
public:
9+
string longestCommonPrefix(vector<string>& str) {
10+
int n = str.size();
11+
if(n==0) return "";
12+
13+
string ans = "";
14+
sort(begin(str), end(str));
15+
string a = str[0];
16+
string b = str[n-1];
17+
18+
for(int i=0; i<a.size(); i++){
19+
if(a[i]==b[i]){
20+
ans = ans + a[i];
21+
}
22+
else{
23+
break;
24+
}
25+
}
26+
27+
return ans;
28+
29+
}
30+
};```

468008578_valid-parentheses.cxx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
id = 468008578
2+
lang = cpp
3+
runtime = 0 ms
4+
memory = 6.3 MB
5+
title_slug = valid-parentheses
6+
code =
7+
```class Solution {
8+
public:
9+
bool isValid(string st) {
10+
stack<char> s;
11+
char x;
12+
13+
for (int i = 0; i < st.length(); i++)
14+
{
15+
if (st[i] == '(' || st[i] == '['
16+
|| st[i] == '{')
17+
{
18+
s.push(st[i]);
19+
continue;
20+
}
21+
22+
if (s.empty())
23+
return false;
24+
25+
switch (st[i]) {
26+
case ')':
27+
28+
x = s.top();
29+
s.pop();
30+
if (x == '{' || x == '[')
31+
return false;
32+
break;
33+
34+
case '}':
35+
36+
x = s.top();
37+
s.pop();
38+
if (x == '(' || x == '[')
39+
return false;
40+
break;
41+
42+
case ']':
43+
44+
x = s.top();
45+
s.pop();
46+
if (x == '(' || x == '{')
47+
return false;
48+
break;
49+
}
50+
}
51+
return (s.empty());
52+
}
53+
};```

468881965_merge-two-sorted-lists.cxx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
id = 468881965
2+
lang = cpp
3+
runtime = 12 ms
4+
memory = 14.8 MB
5+
title_slug = merge-two-sorted-lists
6+
code =
7+
```/**
8+
* Definition for singly-linked list.
9+
* struct ListNode {
10+
* int val;
11+
* ListNode *next;
12+
* ListNode() : val(0), next(nullptr) {}
13+
* ListNode(int x) : val(x), next(nullptr) {}
14+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
15+
* };
16+
*/
17+
class Solution {
18+
public:
19+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
20+
//let's try two pointer methof
21+
ListNode *head = new ListNode(0) ;
22+
23+
ListNode *res = head ;
24+
25+
while(l1 != NULL && l2 != NULL) {
26+
if(l1->val <= l2->val ){
27+
head->next = l1 ;
28+
l1 = l1->next ;
29+
}else {
30+
head->next = l2 ;
31+
l2 = l2->next ;
32+
}
33+
head = head->next ;
34+
}
35+
36+
if(l1 == NULL ){
37+
head->next = l2 ;
38+
}else {
39+
head->next = l1 ;
40+
}
41+
42+
return res->next ;
43+
}
44+
};```

0 commit comments

Comments
 (0)