mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 08:54:07 +00:00
day 2: 69-sqrt-x
also refactored, added drivers
This commit is contained in:
parent
a9d7373732
commit
9da326288a
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.vscode/
|
||||||
|
*.exe
|
||||||
12
69-sqrt-x/driver.cpp
Normal file
12
69-sqrt-x/driver.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int x = 69, answer = 8;
|
||||||
|
Solution soln;
|
||||||
|
cout << "Square root correct? " << soln.test(x, answer) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
32
69-sqrt-x/soln.cpp
Normal file
32
69-sqrt-x/soln.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
int Solution::mySqrt(int x)
|
||||||
|
{
|
||||||
|
// base case
|
||||||
|
if (x == 0 || x == 1)
|
||||||
|
{
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
// effectively only till ~sqrt(x)
|
||||||
|
for (int i = 2; i <= x; i++)
|
||||||
|
{
|
||||||
|
long i_square = long(i) * i;
|
||||||
|
// x is a perfect square
|
||||||
|
if (i_square == x)
|
||||||
|
{
|
||||||
|
return int(i);
|
||||||
|
}
|
||||||
|
// x is not a perfect square
|
||||||
|
else if (i_square > x)
|
||||||
|
{
|
||||||
|
return int(i - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1; // compiler fuss
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Solution::test(int x, int answer)
|
||||||
|
{
|
||||||
|
return answer == mySqrt(x);
|
||||||
|
}
|
||||||
6
69-sqrt-x/soln.hpp
Normal file
6
69-sqrt-x/soln.hpp
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int mySqrt(int x);
|
||||||
|
bool test(int x, int answer);
|
||||||
|
};
|
||||||
14
88-merge-sorted-array/driver.cpp
Normal file
14
88-merge-sorted-array/driver.cpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
vector<int> nums1 = vector<int>{1, 2, 3, 0, 0, 0};
|
||||||
|
vector<int> nums2 = vector<int>{2, 5, 6};
|
||||||
|
vector<int> answer = vector<int>{1, 2, 2, 3, 5, 6};
|
||||||
|
Solution soln;
|
||||||
|
cout << "Merged correctly? " << soln.test(nums1, 3, nums2, 3, answer) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@ -1,13 +1,9 @@
|
|||||||
#include <bits/stdc++.h>
|
#include "soln.hpp"
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using std::vector;
|
using std::vector;
|
||||||
|
|
||||||
class Solution
|
void Solution::merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
|
||||||
{
|
|
||||||
public:
|
|
||||||
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n)
|
|
||||||
{
|
{
|
||||||
int arr1_p = 0, arr2_p = 0;
|
int arr1_p = 0, arr2_p = 0;
|
||||||
vector<int> result = vector<int>(m + n);
|
vector<int> result = vector<int>(m + n);
|
||||||
@ -35,5 +31,11 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
nums1 = result;
|
nums1 = result;
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool Solution::test(vector<int> &nums1, int m, vector<int> &nums2, int n, vector<int> &answer)
|
||||||
|
{
|
||||||
|
vector<int> nums1_copy(nums1);
|
||||||
|
merge(nums1_copy, m, nums2, n);
|
||||||
|
return answer == nums1_copy;
|
||||||
|
}
|
||||||
8
88-merge-sorted-array/soln.hpp
Normal file
8
88-merge-sorted-array/soln.hpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <vector>
|
||||||
|
using std::vector;
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void merge(vector<int> &nums1, int m, vector<int> &nums2, int n);
|
||||||
|
bool test(vector<int> &nums1, int m, vector<int> &nums2, int n, vector<int> &answer);
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user