forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1496-PathCrossing.cs
35 lines (29 loc) · 906 Bytes
/
1496-PathCrossing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//-----------------------------------------------------------------------------
// Runtime: 72ms
// Memory Usage: 22.6 MB
// Link: https://leetcode.com/submissions/detail/359556522/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _1496_PathCrossing
{
public bool IsPathCrossing(string path)
{
var set = new HashSet<(int x, int y)>();
int x = 0, y = 0;
set.Add((x, y));
foreach (var cmd in path)
{
if (cmd == 'N') y++;
else if (cmd == 'S') y--;
if (cmd == 'E') x++;
if (cmd == 'W') x--;
if (set.Contains((x, y)))
return true;
set.Add((x, y));
}
return false;
}
}
}