-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 0029: Divide Two Integers
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn divide(dividend: i32, divisor: i32) -> i32 { | ||
let negative = (dividend < 0) ^ (divisor < 0); | ||
let mut dividend = if dividend == i32::MIN { | ||
u32::from_le_bytes(dividend.to_le_bytes()) | ||
} else { | ||
dividend.unsigned_abs() | ||
}; | ||
let mut divisor = divisor.unsigned_abs(); | ||
let max_offset = divisor.leading_zeros(); | ||
let bound = u32::from_le_bytes(i32::MIN.to_le_bytes()); | ||
|
||
let mut result = 0; | ||
let mut count = 0; | ||
|
||
while dividend >= (divisor << 1) && count < max_offset { | ||
count += 1; | ||
divisor <<= 1; | ||
} | ||
|
||
for offset in (0..=count).rev() { | ||
if dividend >= divisor { | ||
result += 1 << offset; | ||
dividend -= divisor; | ||
} | ||
divisor >>= 1; | ||
} | ||
|
||
if result >= bound { | ||
if negative { | ||
i32::MIN | ||
} else { | ||
i32::MAX | ||
} | ||
} else if negative { | ||
-(result as i32) | ||
} else { | ||
result as i32 | ||
} | ||
} | ||
} | ||
|
||
impl super::Solution for Solution { | ||
fn divide(dividend: i32, divisor: i32) -> i32 { | ||
Self::divide(dividend, divisor) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
pub mod bit_manipulation; | ||
|
||
pub trait Solution { | ||
fn divide(dividend: i32, divisor: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((10, 3), 3), | ||
((7, -3), -2), | ||
((0, 1), 0), | ||
((1, 1), 1), | ||
((1_100_540_749, -1_090_366_779), -1), | ||
((2_147_483_647, 1), 2_147_483_647), | ||
((-2_147_483_648, -1), 2_147_483_647), | ||
((-10, 3), -3), | ||
((-7, -3), 2), | ||
]; | ||
|
||
for ((dividend, divisor), expected) in test_cases { | ||
assert_eq!(S::divide(dividend, divisor), expected); | ||
} | ||
} | ||
} |