rustgym/leetcode/
_1496_path_crossing.rs

1struct Solution;
2use std::collections::HashSet;
3
4impl Solution {
5    fn is_path_crossing(path: String) -> bool {
6        let mut hs: HashSet<(i32, i32)> = HashSet::new();
7        hs.insert((0, 0));
8        let mut x = 0;
9        let mut y = 0;
10        for c in path.chars() {
11            match c {
12                'N' => {
13                    y += 1;
14                }
15                'S' => {
16                    y -= 1;
17                }
18                'E' => {
19                    x += 1;
20                }
21                _ => {
22                    x -= 1;
23                }
24            }
25            if !hs.insert((x, y)) {
26                return true;
27            }
28        }
29        false
30    }
31}
32
33#[test]
34fn test() {
35    let path = "NES".to_string();
36    let res = false;
37    assert_eq!(Solution::is_path_crossing(path), res);
38    let path = "NESWW".to_string();
39    let res = true;
40    assert_eq!(Solution::is_path_crossing(path), res);
41}