Leetcode - Conversion
29. Divide Two Integers[M]
https://leetcode.com/problems/divide-two-integers/
Description
iven two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
Return the quotient after dividing dividend by divisor.
The integer division should truncate toward zero, which means losing its fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) = -2.
Example 1:
1 | Input: dividend = 10, divisor = 3 |
Example 2:
1 | Input: dividend = 7, divisor = -3 |
Note:
- Both dividend and divisor will be 32-bit signed integers.
- The divisor will never be 0.
- Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.
Solution
1 |
371. Sum of Two Integers
https://leetcode.com/problems/sum-of-two-integers/
Description
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example 1:
1 | Input: a = 1, b = 2 |
Example 2:
1 | Input: a = -2, b = 3 |
Solution
hahaha
1 | class Solution: |
1 | class Solution: |
Ok, that’s ridiculous and cheating, ok, now do it without using pre-defined functionslet us dive into the bit operators.
1 | class Solution: |
https://leetcode.com/discuss/111582/java-simple-easy-understand-solution-with-explanation
https://leetcode.com/discuss/111705/one-positive-one-negative-case-successful-for-python-rules
In Python this problem is much different because of the negative number.
1 | class Solution: |
69. Sqrt(x)[E]
https://leetcode.com/problems/sqrtx/
Description
implement int sqrt(int x).
Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
Example 1:
1 | Input: 4 |
Example 2:
1 | Input: 8 |
Solution
1 | class Solution: |
1 | class Solution: |
166. Fraction to Recurring Decimal[M]
https://leetcode.com/problems/fraction-to-recurring-decimal/
Description
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
Example 1:
1 | Input: numerator = 1, denominator = 2 |
Example 2:
1 | Input: numerator = 2, denominator = 1 |
Example 3:
1 | Input: numerator = 2, denominator = 3 |
Solution
1 |
368. Largest Divisible Subset[M]
https://leetcode.com/problems/largest-divisible-subset/
Description
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies:
Si % Sj = 0 or Sj % Si = 0.
If there are multiple solutions, return any subset is fine.
Example 1:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [1,2,4,8] |
Solution
1 |





