Skip to content

Commit 5424e1c

Browse files
LeetCode Medium Math Solutions
1 parent 4484b74 commit 5424e1c

6 files changed

+337
-0
lines changed
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Link: https://leetcode.com/problems/fraction-to-recurring-decimal/
3+
4+
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
5+
6+
If the fractional part is repeating, enclose the repeating part in parentheses.
7+
8+
Example 1:
9+
10+
Input: numerator = 1, denominator = 2
11+
Output: "0.5"
12+
Example 2:
13+
14+
Input: numerator = 2, denominator = 1
15+
Output: "2"
16+
Example 3:
17+
18+
Input: numerator = 2, denominator = 3
19+
Output: "0.(6)"
20+
*/
21+
22+
// Solution
23+
// Uses the approach of long division method.
24+
25+
/*
26+
TC = O(n)
27+
SC = O(n)
28+
*/
29+
30+
class Solution {
31+
public:
32+
string fractionToDecimal(long long int num, long long int den) {
33+
if(num == 0)
34+
return "0";
35+
map<int, int> mp;
36+
string ans = "";
37+
if ((num < 0) ^ (den < 0))
38+
ans.push_back('-');
39+
40+
ans += to_string(abs(num / den));
41+
42+
num = num % den;
43+
44+
long long int deno = abs(den);
45+
long long int nume = abs(num);
46+
47+
if (num == 0)
48+
return ans;
49+
50+
ans += '.';
51+
52+
while (nume != 0) {
53+
if (mp.find(nume) !=mp.end()) {
54+
ans.insert(mp[nume], "(");
55+
ans.push_back(')');
56+
break;
57+
}
58+
mp[nume] = ans.size();
59+
nume *= 10;
60+
ans += to_string(nume / deno);
61+
62+
nume %= deno;
63+
}
64+
return ans;
65+
}
66+
};

171. Excel Sheet Column Number.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Link: https://leetcode.com/problems/excel-sheet-column-number/
3+
4+
Given a column title as appear in an Excel sheet, return its corresponding column number.
5+
6+
For example:
7+
8+
A -> 1
9+
B -> 2
10+
C -> 3
11+
...
12+
Z -> 26
13+
AA -> 27
14+
AB -> 28
15+
...
16+
Example 1:
17+
18+
Input: "A"
19+
Output: 1
20+
Example 2:
21+
22+
Input: "AB"
23+
Output: 28
24+
Example 3:
25+
26+
Input: "ZY"
27+
Output: 701
28+
*/
29+
30+
// Solution
31+
32+
33+
/*
34+
TC = O(n)
35+
SC = O(1)
36+
*/
37+
38+
class Solution {
39+
public:
40+
int titleToNumber(string s) {
41+
int i, j, len = s.size();
42+
int k = 0, ans = 0;
43+
char ch;
44+
while (s.size()) {
45+
ch = s[s.size() - 1];
46+
ans += pow (26, k) * int(ch -'A' + 1);
47+
k++;
48+
s.pop_back();
49+
}
50+
return ans;
51+
}
52+
};

172. Factorial Trailing Zeroes.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Link: https://leetcode.com/problems/factorial-trailing-zeroes/
3+
4+
Given an integer n, return the number of trailing zeroes in n!.
5+
6+
Example 1:
7+
8+
Input: 3
9+
Output: 0
10+
Explanation: 3! = 6, no trailing zero.
11+
Example 2:
12+
13+
Input: 5
14+
Output: 1
15+
Explanation: 5! = 120, one trailing zero.
16+
Note: Your solution should be in logarithmic time complexity.
17+
*/
18+
19+
// Solution
20+
21+
22+
/*
23+
TC = O(log n base 5)
24+
SC = O(1) in iterative; recursion uses extra memory for execution, here it is O(log n base 5)
25+
*/
26+
27+
class Solution {
28+
public:
29+
int trailingZeroes(int n) {
30+
// This line is solution using recursion
31+
// return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
32+
33+
// Iterative
34+
int i = 1, ans = 0;
35+
while (1) {
36+
if (n / pow(5, i) < 1)
37+
break;
38+
ans += n / pow(5, i++);
39+
}
40+
41+
return ans;
42+
}
43+
};

29. Divide Two Integers.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Link: https://leetcode.com/problems/divide-two-integers/
3+
4+
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
5+
6+
Return the quotient after dividing dividend by divisor.
7+
8+
The integer division should truncate toward zero, which means losing its fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) = -2.
9+
10+
Example 1:
11+
12+
Input: dividend = 10, divisor = 3
13+
Output: 3
14+
Explanation: 10/3 = truncate(3.33333..) = 3.
15+
Example 2:
16+
17+
Input: dividend = 7, divisor = -3
18+
Output: -2
19+
Explanation: 7/-3 = truncate(-2.33333..) = -2.
20+
Note:
21+
22+
Both dividend and divisor will be 32-bit signed integers.
23+
The divisor will never be 0.
24+
*/
25+
26+
// Solution
27+
// We subtract the sum of all powers of two less than the current dividend from it.
28+
// and repeat the process untill dividend is less than divisor.
29+
30+
/*
31+
TC = O(1)
32+
SC = O(1)
33+
*/
34+
35+
class Solution {
36+
public:
37+
int divide(long long int dividend, long long int divisor) {
38+
39+
long long ans = 0, temp = 1;
40+
41+
int flag = (divisor < 0 ^ dividend < 0);
42+
43+
if (dividend == INT_MIN && divisor == -1)
44+
return INT_MAX;
45+
46+
if (dividend == INT_MIN && divisor == 1)
47+
return INT_MIN;
48+
49+
dividend = abs(dividend);
50+
divisor = abs(divisor);
51+
52+
while (dividend >= divisor) {
53+
long long int temp = 1;
54+
ans += temp;
55+
long long int div = divisor;
56+
dividend -= divisor;
57+
while (dividend >= divisor << 1) {
58+
divisor <<= 1;
59+
dividend = dividend - divisor;
60+
temp <<= 1;
61+
62+
ans += temp;
63+
}
64+
65+
divisor = div;
66+
}
67+
if (flag)
68+
return -ans;
69+
return ans;
70+
}
71+
};

50. Pow(x, n).cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Link: https://leetcode.com/problems/powx-n/
3+
Implement pow(x, n), which calculates x raised to the power n (xn).
4+
5+
Example 1:
6+
7+
Input: 2.00000, 10
8+
Output: 1024.00000
9+
Example 2:
10+
11+
Input: 2.10000, 3
12+
Output: 9.26100
13+
Example 3:
14+
15+
Input: 2.00000, -2
16+
Output: 0.25000
17+
Explanation: 2-2 = 1/22 = 1/4 = 0.25
18+
Note:
19+
20+
-100.0 < x < 100.0
21+
n is a 32-bit signed integer, within the range [−231, 231 − 1]
22+
23+
*/
24+
25+
// Solution
26+
//Attempt is to reduce the number of calls to pow function
27+
// and that is being done by call the pow function recursively on sqaure of the number.
28+
29+
/*
30+
TC = O(log n)
31+
SC = O(log n) for stack
32+
*/
33+
34+
class Solution {
35+
public:
36+
double myPow(double x, int n) {
37+
if (n == 0)
38+
return 1;
39+
if(n == INT_MIN){
40+
x = x * x;
41+
n = n/2;
42+
}
43+
if (n < 0) {
44+
n = -n;
45+
x = 1 / x;
46+
}
47+
48+
return (n%2) ? x * pow (x * x, n / 2) : pow (x * x, n / 2);
49+
}
50+
};

69. Sqrt(x).cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Link: https://leetcode.com/problems/sqrtx/
3+
4+
Implement int sqrt(int x).
5+
6+
Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
7+
8+
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
9+
10+
Example 1:
11+
12+
Input: 4
13+
Output: 2
14+
Example 2:
15+
16+
Input: 8
17+
Output: 2
18+
Explanation: The square root of 8 is 2.82842..., and since
19+
the decimal part is truncated, 2 is returned.
20+
*/
21+
22+
// Solution
23+
24+
25+
/*
26+
TC = O(log n)
27+
SC = O(1)
28+
*/
29+
30+
class Solution {
31+
public:
32+
int mySqrt(int x) {
33+
if (!x)
34+
return x;
35+
36+
int ans, low = 1, high = x;
37+
int mid;
38+
// binary search
39+
while (1) {
40+
mid = low + (high - low) / 2;
41+
42+
if (mid == x / mid)
43+
break;
44+
45+
if (mid > x / mid)
46+
high = mid - 1;
47+
else {
48+
if (mid + 1 > x / (mid + 1))
49+
return mid;
50+
low = mid + 1;
51+
}
52+
}
53+
return mid;
54+
}
55+
};

0 commit comments

Comments
 (0)