From 3b294b921bfe02099b294c6c3d61583019a3e198 Mon Sep 17 00:00:00 2001 From: "Paulo H. Lamounier" <53798700+Nanashii76@users.noreply.github.com> Date: Tue, 21 May 2024 20:17:25 -0300 Subject: [PATCH] more some leetcode problems --- .../problemsets/add_two_numbers.cpp | 37 +++++++++++++++++ .../longest_substring_without_repeating.cpp | 41 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 leetcode_problemset/problemsets/add_two_numbers.cpp create mode 100644 leetcode_problemset/problemsets/longest_substring_without_repeating.cpp diff --git a/leetcode_problemset/problemsets/add_two_numbers.cpp b/leetcode_problemset/problemsets/add_two_numbers.cpp new file mode 100644 index 0000000..e956b2b --- /dev/null +++ b/leetcode_problemset/problemsets/add_two_numbers.cpp @@ -0,0 +1,37 @@ +#include +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; +} diff --git a/leetcode_problemset/problemsets/longest_substring_without_repeating.cpp b/leetcode_problemset/problemsets/longest_substring_without_repeating.cpp new file mode 100644 index 0000000..3ab2808 --- /dev/null +++ b/leetcode_problemset/problemsets/longest_substring_without_repeating.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +pair check_substring(string comp, int k) { + + int size = comp.length(); + unordered_map 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 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; +// } \ No newline at end of file