mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
rs: add “1496. Path Crossing”
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
d207204281
commit
835b0208b9
1 changed files with 27 additions and 0 deletions
27
rs/path-crossing.rs
Normal file
27
rs/path-crossing.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in a new issue