Skip to content

Commit

Permalink
more some leetcode problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 authored May 21, 2024
1 parent 762f0a9 commit 3b294b9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
37 changes: 37 additions & 0 deletions leetcode_problemset/problemsets/add_two_numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;

struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};

ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{

ListNode *l3 = new ListNode(0);
ListNode *current = l3;
int carry = 0;

while (l1 != NULL || l2 != NULL || carry != 0)
{
int sum = carry;
if (l1 != NULL)
{
sum += l1->val;
l1 = l1->next;
}
if (l2 != NULL)
{
sum += l2->val;
l2 = l2->next;
}
carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current->next;
}

return l3->next;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <bits/stdc++.h>
using namespace std;

pair<int,int> check_substring(string comp, int k) {

int size = comp.length();
unordered_map<char,int> rep;
for(int i = k; i < size; ++i) {
rep[comp[i]]++;
if(rep[comp[i]] >= 2)
return {k,i};
}

return {k,size};

}

int lengthOfLongestSubstring(string s) {

int ini = 0, size = s.length(), fin = 0;
for(int i = 0; i < size; ++i) {
pair<int,int> new_fin = check_substring(s,i);
if((new_fin.second - new_fin.first) > (fin-ini)) {
fin = new_fin.second;
ini = new_fin.first;
}
//cout << ini << " " << fin << endl;
}

return fin-ini;
}


// int main(){

// string s = "abcabcbb";
// int maxSub = lengthOfLongestSubstring(s);
// cout << maxSub << endl;

// return 0;
// }

0 comments on commit 3b294b9

Please sign in to comment.