From 0ead726de56debaf9ebf22e94f7dd0685b294eff Mon Sep 17 00:00:00 2001 From: "Paulo H. Lamounier" <53798700+Nanashii76@users.noreply.github.com> Date: Tue, 11 Jun 2024 09:28:20 -0300 Subject: [PATCH] some leetcode problems --- .../problemsets/longestValidParentheses.cpp | 23 +++++++++++++++++ .../problemsets/palindromeNumber.cpp | 23 +++++++++++++++++ .../problemsets/relativeSortArray.cpp | 22 ++++++++++++++++ .../problemsets/removeDuplicates.cpp | 15 +++++++++++ .../problemsets/reverseInteger.cpp | 25 +++++++++++++++++++ .../problemsets/stringToInteger.cpp | 18 +++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 leetcode_problemset/problemsets/longestValidParentheses.cpp create mode 100644 leetcode_problemset/problemsets/palindromeNumber.cpp create mode 100644 leetcode_problemset/problemsets/relativeSortArray.cpp create mode 100644 leetcode_problemset/problemsets/removeDuplicates.cpp create mode 100644 leetcode_problemset/problemsets/reverseInteger.cpp create mode 100644 leetcode_problemset/problemsets/stringToInteger.cpp diff --git a/leetcode_problemset/problemsets/longestValidParentheses.cpp b/leetcode_problemset/problemsets/longestValidParentheses.cpp new file mode 100644 index 0000000..20bca16 --- /dev/null +++ b/leetcode_problemset/problemsets/longestValidParentheses.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int longestValidParenthesis(string s) { + stack st; + st.push(-1); + int maxCount = 0; + + for(int i = 0; i < s.length(); ++i) { + if(s[i]=='(') + st.push(i); + else + st.pop(); + + if(st.empty()) + st.push(i); + else + maxCount = max(maxCount, i - st.top()); + + } + + return maxCount; +} \ No newline at end of file diff --git a/leetcode_problemset/problemsets/palindromeNumber.cpp b/leetcode_problemset/problemsets/palindromeNumber.cpp new file mode 100644 index 0000000..cf736e0 --- /dev/null +++ b/leetcode_problemset/problemsets/palindromeNumber.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +typedef long long ll; + +bool isPalindrome(int x) { + + if(x < 0) + return false; + + ll ans = 0; + int aux = x; + while(x) { + ans = (10*ans)+(x%10); + x /= 10; + } + + if(ans > INT_MAX or ans < INT_MIN) return false; + if(aux == ans) return true; + + return false; + +} \ No newline at end of file diff --git a/leetcode_problemset/problemsets/relativeSortArray.cpp b/leetcode_problemset/problemsets/relativeSortArray.cpp new file mode 100644 index 0000000..c1e0e71 --- /dev/null +++ b/leetcode_problemset/problemsets/relativeSortArray.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +vector relativeSortArray(vector &arr1, vector &arr2) { + vector sorted; + int n = arr1.size(), m = arr2.size(); + sort(arr1.begin(), arr1.end()); + for(int i = 0, j = 0; i < m; ++i) { + for(int j = 0; j < n; ++j) { + if(arr1[j] == arr2[i]) { + sorted.emplace_back(arr1[j]); + arr1[j] = -1; + } + } + } + + for(int i = 0; i < n; ++i) + if(arr1[i] != -1) + sorted.emplace_back(arr1[i]); + + return sorted; +} \ No newline at end of file diff --git a/leetcode_problemset/problemsets/removeDuplicates.cpp b/leetcode_problemset/problemsets/removeDuplicates.cpp new file mode 100644 index 0000000..fbda2f0 --- /dev/null +++ b/leetcode_problemset/problemsets/removeDuplicates.cpp @@ -0,0 +1,15 @@ +#include +using namespace std; + +int removeDuplicates(vector &nums) { + set s; + int size = nums.size(); + for(int i = 0; i < size; ++i) + s.insert(nums[i]); + + int i = 0; + for(auto x : s) + nums[i++] = x; + + return s.size(); +} \ No newline at end of file diff --git a/leetcode_problemset/problemsets/reverseInteger.cpp b/leetcode_problemset/problemsets/reverseInteger.cpp new file mode 100644 index 0000000..b0041dc --- /dev/null +++ b/leetcode_problemset/problemsets/reverseInteger.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +typedef long long ll; + +int reverse(int x) { + ll ans = 0; + while (x) + { + ans = (ans * 10) + (x % 10); + x /= 10; + } + + if (ans > INT_MAX or ans < INT_MIN) + return 0; + return ans; +} + +int main() { + int value; + cin >> value; + cout << reverse(value) << endl; + + return 0; +} \ No newline at end of file diff --git a/leetcode_problemset/problemsets/stringToInteger.cpp b/leetcode_problemset/problemsets/stringToInteger.cpp new file mode 100644 index 0000000..0a45ad7 --- /dev/null +++ b/leetcode_problemset/problemsets/stringToInteger.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; + +int myAtoi(string s) { + + int ans = 0, size = s.length(); + + // checking if is readable + for(int i = 0; i < size; ++i) + if(isalpha(s[i]) and isalpha(s[i+1])) + return 0; + + for(int i = 0; i < size; ++i) + if(isdigit(s[i])) + ans = ans*10+(s[i]-'0'); + + return s[0]==-1?-ans:ans; +} \ No newline at end of file