From df57a6227524b51fd171e2a9bfbb7c5e818ea57b Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Fri, 20 Oct 2023 20:58:44 -0700 Subject: [PATCH] some more neetcode --- 1-two-sum/driver.cpp | 11 +++++++++++ 1-two-sum/soln.cpp | 24 ++++++++++++++++++++++++ 1-two-sum/soln.hpp | 8 ++++++++ 125-valid-palindrome/driver.cpp | 10 ++++++++++ 125-valid-palindrome/soln.cpp | 32 ++++++++++++++++++++++++++++++++ 125-valid-palindrome/soln.hpp | 8 ++++++++ 20-valid-parentheses/driver.cpp | 3 ++- 20-valid-parentheses/soln.cpp | 3 ++- 8 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 1-two-sum/driver.cpp create mode 100644 1-two-sum/soln.cpp create mode 100644 1-two-sum/soln.hpp create mode 100644 125-valid-palindrome/driver.cpp create mode 100644 125-valid-palindrome/soln.cpp create mode 100644 125-valid-palindrome/soln.hpp diff --git a/1-two-sum/driver.cpp b/1-two-sum/driver.cpp new file mode 100644 index 0000000..f59a29d --- /dev/null +++ b/1-two-sum/driver.cpp @@ -0,0 +1,11 @@ +#include "soln.hpp" + +int main() +{ + vector nums{2, 7, 11, 15}; + int target = 9; + vector answer{0, 1}; + Solution soln; + cout << "Found two-sum indices correctly? " << soln.test(nums, target, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/1-two-sum/soln.cpp b/1-two-sum/soln.cpp new file mode 100644 index 0000000..d99ad6f --- /dev/null +++ b/1-two-sum/soln.cpp @@ -0,0 +1,24 @@ +#include "soln.hpp" + +vector Solution::twoSum(vector &nums, int target) +{ + vector result(2); + for (int i = 0; i < nums.size(); i++) + { + for (int j = i + 1; j < nums.size(); j++) + { + if (nums[i] + nums[j] == target) + { + result[0] = i; + result[1] = j; + return result; + } + } + } + return result; +} + +bool Solution::test(vector &nums, int target, vector &answer) +{ + return twoSum(nums, target) == answer; +} \ No newline at end of file diff --git a/1-two-sum/soln.hpp b/1-two-sum/soln.hpp new file mode 100644 index 0000000..1573843 --- /dev/null +++ b/1-two-sum/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + vector twoSum(vector &nums, int target); + bool test(vector &nums, int target, vector &answer); +}; \ No newline at end of file diff --git a/125-valid-palindrome/driver.cpp b/125-valid-palindrome/driver.cpp new file mode 100644 index 0000000..63eed10 --- /dev/null +++ b/125-valid-palindrome/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + string s = "A man, a plan, a canal: Panama"; + bool answer = true; + Solution soln; + cout << "Checked palindrome correctly? " << soln.test(s, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/125-valid-palindrome/soln.cpp b/125-valid-palindrome/soln.cpp new file mode 100644 index 0000000..524c958 --- /dev/null +++ b/125-valid-palindrome/soln.cpp @@ -0,0 +1,32 @@ +#include "soln.hpp" + +bool Solution::isPalindrome(string s) +{ + int n = s.length(); + if (n < 2) + return true; + for (int i = 0; i < n; i++) + { + if (s[i] >= 'A' && s[i] <= 'Z') + s[i] += 32; + if ((s[i] < 'a' || s[i] > 'z') && + (s[i] < '0' || s[i] > '9')) + { + s.erase(s.begin() + i); + i--; + n--; + } + } + n = s.length(); + for (int i = 0, j = n - 1; i < n / 2; i++, j--) + { + if (s[i] != s[n - i - 1]) + return false; + } + return true; +} + +bool Solution::test(string s, bool answer) +{ + return isPalindrome(s) == answer; +} \ No newline at end of file diff --git a/125-valid-palindrome/soln.hpp b/125-valid-palindrome/soln.hpp new file mode 100644 index 0000000..643edc1 --- /dev/null +++ b/125-valid-palindrome/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + bool isPalindrome(string s); + bool test(string s, bool answer); +}; \ No newline at end of file diff --git a/20-valid-parentheses/driver.cpp b/20-valid-parentheses/driver.cpp index 5df6f25..ac5c7bc 100644 --- a/20-valid-parentheses/driver.cpp +++ b/20-valid-parentheses/driver.cpp @@ -1,6 +1,7 @@ #include "soln.hpp" -int main() { +int main() +{ string s = "({[[[]]]})"; bool answer = true; Solution soln; diff --git a/20-valid-parentheses/soln.cpp b/20-valid-parentheses/soln.cpp index 231c433..d60942f 100644 --- a/20-valid-parentheses/soln.cpp +++ b/20-valid-parentheses/soln.cpp @@ -37,6 +37,7 @@ bool Solution::isValid(string s) return bstack.size() == 0; } -bool Solution::test(string s, bool answer) { +bool Solution::test(string s, bool answer) +{ return isValid(s) == answer; } \ No newline at end of file