diff --git a/src/lib.rs b/src/lib.rs index 618f1c1..b8e3e0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -386,6 +386,7 @@ pub mod problem_0648_replace_words; pub mod problem_0650_2_keys_keyboard; pub mod problem_0653_two_sum_iv_input_is_a_bst; pub mod problem_0654_maximum_binary_tree; +pub mod problem_0657_robot_return_to_origin; pub mod problem_0665_non_decreasing_array; pub mod problem_0670_maximum_swap; pub mod problem_0673_number_of_longest_increasing_subsequence; diff --git a/src/problem_0657_robot_return_to_origin/iterative.rs b/src/problem_0657_robot_return_to_origin/iterative.rs new file mode 100644 index 0000000..5104535 --- /dev/null +++ b/src/problem_0657_robot_return_to_origin/iterative.rs @@ -0,0 +1,31 @@ +pub struct Solution; + +impl Solution { + pub fn judge_circle(moves: String) -> bool { + let mut pos = (0, 0); + for ch in moves.chars() { + match ch { + 'R' => pos.0 += 1, + 'L' => pos.0 -= 1, + 'U' => pos.1 += 1, + 'D' => pos.1 -= 1, + _ => unreachable!(), + } + } + pos == (0, 0) + } +} + +impl super::Solution for Solution { + fn judge_circle(moves: String) -> bool { + Self::judge_circle(moves) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_0657_robot_return_to_origin/mod.rs b/src/problem_0657_robot_return_to_origin/mod.rs new file mode 100644 index 0000000..8b1c67a --- /dev/null +++ b/src/problem_0657_robot_return_to_origin/mod.rs @@ -0,0 +1,23 @@ +pub mod iterative; + +pub trait Solution { + fn judge_circle(moves: String) -> bool; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ("UD", true), + ("LL", false), + ("RRDD", false), + ("LDRRLRUULR", false), + ]; + + for (moves, expected) in test_cases { + assert_eq!(S::judge_circle(moves.to_string()), expected); + } + } +}