Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cpp files in leetcode #400

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
'''
4. Median of Two Sorted Arrays

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).
'''
*/

class Solution
{
public:
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2)
{
for (auto i : nums2)
nums1.push_back(i);

sort(begin(nums1), end(nums1));

for (auto i : nums1)
cout << i << " ";

int size = nums1.size() - 1;

if (size % 2 == 0)
return nums1[size / 2];
else
return (nums1[size / 2] + nums1[size / 2 + 1]) / 2.0;
return 0;
}
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the number
0029 to 0209

And also, if you have multiple solutions then numbers like
0209 (i)] & 0209 (ii)]
And split the code in 2 files

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
'''
209. Minimum Size Subarray Sum

Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target.
If there is no such subarray, return 0 instead.
'''
*/

class Solution
{
public:
int minSubArrayLen(int target, vector<int> &nums)
{
int n = (int)nums.size();
int sum = 0, j = 0;
int res = INT_MAX;

for (int i = 0; i < n; i++)
{
sum += nums[i];
while (sum >= target)
{
res = min(res, i - j + 1);
sum -= nums[j++];
}
}
return res == INT_MAX ? 0 : res;
}
};

//
class Solution
{
public:
int minSubArrayLen(int s, vector<int> &nums)
{
int n = nums.size(),
len = INT_MAX;
vector<int> sums(n + 1, 0);

for (int i = 1; i <= n; i++)
{
sums[i] = sums[i - 1] + nums[i - 1];
}

for (int i = n; i >= 0 && sums[i] >= s; i--)
{
int j = upper_bound(sums.begin(), sums.end(), sums[i] - s) - sums.begin();
len = min(len, i - j + 1);
}
return len == INT_MAX ? 0 : len;
}
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split the codes in 3 files

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
50. Pow(x, n)

Implement pow(x, n), which calculates x raised to the power n (i.e., xn).
*/

class Solution
{
public:
double myPow(double x, int n)
{
return pow(x, n);
}
};




//

class Solution
{
public:
double myPower(double x, long long N)
{
if (N == 0)
return 1.0;
double y = myPower(x, N / 2);
return N % 2 == 0 ? y * y : y * y * x;
}

double myPow(double x, int n)
{
long long N = n;
return N >= 0 ? myPower(x, N) : 1.0 / myPower(x, -N);
}
};


//fast
class Solution
{
public:
double myPow(double x, int n)
{
if (n == 0)
return 1;
if (n < 0)
{
n = abs(n);
x = 1 / x;
}
if (n % 2 == 0)
return myPow(x * x, n / 2);
else
return x * myPow(x, n - 1);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
'''
55. Jump Game

You are given an integer array nums. You are initially positioned at the array's first index,
and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.

Example 1:

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
'''
*/

class Solution
{
public
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add :

boolean canJump(int[] nums)
{
// Take curr variable to keep the current maximum jump...
int curr = 0;
// Traverse all the elements through loop...
for (int i = 0; i < nums.length; i++)
{
// If the current index 'i' is less than current maximum jump 'curr'...
// It means there is no way to jump to current index...
// so we should return false...
if (i > curr)
{
return false;
}
// Update the current maximum jump...
curr = Math.max(curr, i + nums[i]); // It’s possible to reach the end of the array...
}
return true;
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add ;

Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
'''
66. Plus One

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer.
The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.

Increment the large integer by one and return the resulting array of digits.
'''
*/

class Solution
{
public:
vector<int> plusOne(vector<int> &digits)
{
long long num = 0;
for (auto i : digits)
num = num * 10 + i;
num++;
digits.clear();
while (num)
{
int n = num % 10;
digits.emplace(begin(digits), n);
num /= 10;
}
cout << num;
return digits;
}
};


//
class Solution
{
public:
vector<int> plusOne(vector<int> &digits)
{
int sz = digits.size() - 1;
cout << sz;
/*if (sz==0 && digits[sz]==9)
{
digits[sz] = 0;
digits.emplace(digits.begin(), 1);
return digits;
}
else if (sz==0)
{
int n=digits[sz];
digits[sz] = n+1;
return digits;
}*/
while (sz >= 0)
{

if (digits[sz] <= 8)
{
int n = digits[sz];
digits[sz] = n + 1;
break;
}
else
{
digits[sz] = 0;
sz--;
}
if (sz == -1)
{
digits.emplace(digits.begin(), 1);
}
}
return digits;
}
};

//
class Solution
{
public:
vector<int> plusOne(vector<int> &v)
{
int n = v.size();
for (int i = n - 1; i >= 0; i--)
{
if (i == n - 1)
v[i]++;
if (v[i] == 10)
{
v[i] = 0;
if (i != 0)
{
v[i - 1]++;
}
else
{
v.push_back(0);
v[i] = 1;
}
}
}
return v;
}
};

//
class Solution
{
public:
vector<int> plusOne(vector<int> &digits)
{
int n = digits.size();
for (int i = n - 1; i >= 0; i--)
{
if (i == n - 1)
{
digits[i]++;
}
if (digits[i] == 10)
{
digits[i] = 0;
if (i != 0)
{
digits[i - 1] += 1;
}
else
{
digits.insert(digits.begin(), 1);
}
}
}
return digits;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
'''
69. Sqrt(x)

Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.

You must not use any built-in exponent function or operator.

For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
'''
*/

class Solution
{
public:
int mySqrt(int x)
{
double i = 0;
while (i * i <= x)
{
if ((i * i) <= x && (i + 1) * (i + 1) > x)
return int(i);
i++;
}
return 0;
}
};

//
class Solution
{
public:
int mySqrt(int num)
{
if (num <= 1) // square root of 0 is 0, square root of 1 is 1, square root of a negative is an imaginary.
return num;

double tolerance = 1e-7;

double x0 = num;
double x = x0 - (x0 * x0 - num) / (2 * x0);

while (abs(x - x0) > tolerance)
{
x0 = x;
x = x0 - (x0 * x0 - num) / (2 * x0);
}

return (int)x;
}
};
Loading
Loading