1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/rs/path-crossing.rs
Matej Focko 48eae7f00b
chore: run pre-commit
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-01-19 07:12:37 +01:00

26 lines
602 B
Rust

use std::collections::HashSet;
impl Solution {
pub fn is_path_crossing(path: String) -> bool {
let mut visited = HashSet::from([(0, 0)]);
let mut x = 0;
let mut y = 0;
for d in path.chars() {
match d {
'N' => y -= 1,
'S' => y += 1,
'E' => x += 1,
'W' => x -= 1,
_ => unreachable!("invalid direction"),
}
if visited.contains(&(x, y)) {
return true;
}
visited.insert((x, y));
}
false
}
}