generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem60.cs
More file actions
50 lines (42 loc) · 1.51 KB
/
Problem60.cs
File metadata and controls
50 lines (42 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/divide-two-integers/">Divide Two Integers</see>.
/// </summary>
public static class Problem60
{
private const double EPS = 1e-9;
/// <summary>
/// Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
/// The integer division should truncate toward zero, which means losing its fractional part.
/// For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2.
/// Return the quotient after dividing dividend by divisor.
/// Time complexity: O(1).
/// Space complexity: O(1).
/// </summary>
/// <param name="dividend">Dividend.</param>
/// <param name="divisor">Divisor.</param>
/// <returns>Quotient after dividing dividend by divisor.</returns>
public static int Divide(int dividend, int divisor)
{
if (divisor == int.MinValue)
{
return dividend == divisor ? 1 : 0;
}
var result = 0;
if (dividend == int.MinValue)
{
if (divisor == 1)
{
return int.MinValue;
}
if (divisor == -1)
{
return int.MaxValue;
}
dividend += Math.Abs(divisor);
result++;
}
result += (int)(Math.Exp(Math.Log(Math.Abs(dividend)) - Math.Log(Math.Abs(divisor))) + EPS);
return (dividend > 0) == (divisor > 0) ? result : -result;
}
}