29. Divide Two Integers Medium

@problem@discussion
#Math#Bit Manipulation



1/**
2 * [29] Divide Two Integers
3 *
4 * Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
5 * 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.
6 * Return the quotient after dividing dividend by divisor.
7 * Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [-2^31, 2^31 - 1]. For this problem, if the quotient is strictly greater than 2^31 - 1, then return 2^31 - 1, and if the quotient is strictly less than -2^31, then return -2^31.
8 *  
9 * Example 1:
10 * 
11 * Input: dividend = 10, divisor = 3
12 * Output: 3
13 * Explanation: 10/3 = 3.33333.. which is truncated to 3.
14 * 
15 * Example 2:
16 * 
17 * Input: dividend = 7, divisor = -3
18 * Output: -2
19 * Explanation: 7/-3 = -2.33333.. which is truncated to -2.
20 * 
21 *  
22 * Constraints:
23 * 
24 * 	-2^31 <= dividend, divisor <= 2^31 - 1
25 * 	divisor != 0
26 * 
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/divide-two-integers/
31// discuss: https://leetcode.com/problems/divide-two-integers/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36    pub fn divide(dividend: i32, divisor: i32) -> i32 {
37        0
38    }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_29() {
49    }
50}
51


Back
© 2025 bowen.ge All Rights Reserved.