Skip to content

Commit

Permalink
Add problem 0657: Robot Return to Origin
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed Jun 30, 2024
1 parent f30021f commit 084c84d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions src/problem_0657_robot_return_to_origin/iterative.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
23 changes: 23 additions & 0 deletions src/problem_0657_robot_return_to_origin/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
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);
}
}
}

0 comments on commit 084c84d

Please sign in to comment.