当前位置:网站首页>leetcode之判断路径是否相交
leetcode之判断路径是否相交
2020-11-07 21:43:00 【go4it】
序
本文主要记录一下leetcode之判断路径是否相交
题目
给你一个字符串 path,其中 path[i] 的值可以是 'N'、'S'、'E' 或者 'W',分别表示向北、向南、向东、向西移动一个单位。
机器人从二维平面上的原点 (0, 0) 处开始出发,按 path 所指示的路径行走。
如果路径在任何位置上出现相交的情况,也就是走到之前已经走过的位置,请返回 True ;否则,返回 False 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/path-crossing
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
class Solution {
public boolean isPathCrossing(String path) {
int x = 0;
int y = 0;
Set<String> pathSet = new HashSet<String>();
pathSet.add("00");
for (char c : path.toCharArray()) {
if (c == 'N') {
y++;
} else if (c == 'S') {
y--;
} else if (c == 'W') {
x--;
} else if (c == 'E') {
x++;
}
String p = String.valueOf(x) + String.valueOf(y);
if (pathSet.contains(p)) {
return true;
}
pathSet.add(p);
}
return false;
}
}
小结
这里维护走过的点,然后遍历path的字符,对x,y坐标进行相应移动,每次移动之后都判断下该点是否走过,走过则返回true,没有则将改点记录到走过的的点中,遍历完都没有符合条件就返回false。
doc
版权声明
本文为[go4it]所创,转载请带上原文链接,感谢
https://my.oschina.net/go4it/blog/4707740
边栏推荐
- How to deal with data leakage and deletion related to business life and death?
- [original] the influence of arm platform memory and cache on the real-time performance of xenomai
- Ubuntu下搜狗输入法的下载安装及配置
- Exploration and practice of growingio responsive programming
- Android Basics - RadioButton (radio button)
- More than 50 object detection datasets from different industries
- 微信小程序request报400错误 @RequestBody接收不到
- Get started, GIT
- awk实现类sql的join操作
- Data transmission of asynchronous serial communication controlled by group bus communication
猜你喜欢
[original] the influence of arm platform memory and cache on the real-time performance of xenomai
Delphi10's rest.json And system.json Step on the pit
What do you think of the most controversial programming ideas?
The instanceof operator in ecmascript7 specification
Ac86u KX Online
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
Download, installation and configuration of Sogou input method in Ubuntu
laravel8更新之维护模式改进
Face recognition: attack types and anti spoofing techniques
一万四千字分布式事务原理解析,全部掌握你还怕面试被问?
随机推荐
【解决方案】分布式定时任务解决方案
团灭 LeetCode 股票买卖问题
On the coverage technology and best practice of go code
Supervisor process management installation and use
Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
Web安全(一)---浏览器同源策略
Everything is 2020, LINQ query you are still using expression tree
爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】
Download, installation and configuration of Sogou input method in Ubuntu
Search and replace of sed
use Xunit.DependencyInjection Transformation test project
Stack bracket matching
There's not much time left for Kwai Chung.
Idea - the. IML file was not automatically generated by the project
How did I lose control of the team?
Adobe media encoder / me 2021 software installation package (with installation tutorial)
Using subprocess residue in supervisor and python multiprocessing
Animation techniques and details you may not know
Big data algorithm - bloon filter
sed之查找替换