-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission - Path Crossing (javascript)
- Loading branch information
1 parent
e1ef27d
commit f7ffc70
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @param {string} path | ||
* @return {boolean} | ||
*/ | ||
var isPathCrossing = function(path) { | ||
// Initialize starting position and a set to store visited points | ||
let x = 0, y = 0, point = `${x} ${y}`; | ||
const set = new Set([point]) | ||
|
||
for(direction of path){ | ||
// Update position based on the current direction | ||
if(direction == "N"){ | ||
++x; | ||
} | ||
else if(direction == "E"){ | ||
++y; | ||
} | ||
else if(direction == "W"){ | ||
--y; | ||
} | ||
else if(direction == "S"){ | ||
--x | ||
} | ||
|
||
point = `${x} ${y}`; | ||
// Check if the point has been visited before | ||
if(set.has(point)){ | ||
return true | ||
} | ||
else{ | ||
// If the point is not visited, add it to the set | ||
set.add(point) | ||
} | ||
} | ||
// If no crossing occurred during the entire path, return false | ||
return false | ||
}; |