Skip to content

Commit

Permalink
some leetcode problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 authored Jun 11, 2024
1 parent 480aa07 commit 0ead726
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
23 changes: 23 additions & 0 deletions leetcode_problemset/problemsets/longestValidParentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
using namespace std;

int longestValidParenthesis(string s) {
stack<int> 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;
}
23 changes: 23 additions & 0 deletions leetcode_problemset/problemsets/palindromeNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bits/stdc++.h>
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;

}
22 changes: 22 additions & 0 deletions leetcode_problemset/problemsets/relativeSortArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <bits/stdc++.h>
using namespace std;

vector<int> relativeSortArray(vector<int> &arr1, vector<int> &arr2) {
vector<int> 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;
}
15 changes: 15 additions & 0 deletions leetcode_problemset/problemsets/removeDuplicates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <bits/stdc++.h>
using namespace std;

int removeDuplicates(vector<int> &nums) {
set<int> 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();
}
25 changes: 25 additions & 0 deletions leetcode_problemset/problemsets/reverseInteger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <bits/stdc++.h>
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;
}
18 changes: 18 additions & 0 deletions leetcode_problemset/problemsets/stringToInteger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <bits/stdc++.h>
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;
}

0 comments on commit 0ead726

Please sign in to comment.