当前位置:网站首页>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
边栏推荐
- use Xunit.DependencyInjection Transformation test project
- What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
- ROS学习---远程启动ROS节点
- Face recognition: attack types and anti spoofing techniques
- 【C++学习笔记】C++ 标准库 std::thread 的简单使用,一文搞定还不简单?
- 获取树形菜单列表
- Improvement of maintenance mode of laravel8 update
- CPP (1) installation of cmake
- Go之发送钉钉和邮箱
- Writing method of field and field comparison condition in where condition in thinkphpp6
猜你喜欢
android基础-RadioButton(单选按钮)
[C + + learning notes] how about the simple use of the C + + standard library STD:: thread?
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
Code Review Best Practices
Let's talk about the locks in the database
Reflection on a case of bus card being stolen and swiped
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
Android Basics - RadioButton (radio button)
How to choose a good company
来自不同行业领域的50多个对象检测数据集
随机推荐
京淘项目day09
Awk implements SQL like join operation
Git代码提交操作,以及git push提示failed to push some refs'XXX'
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
awk实现类sql的join操作
年薪90万程序员不如月入3800公务员?安稳与高收入,到底如何选择?
Go之发送钉钉和邮箱
Supervisor process management installation and use
栈-括号的匹配
Everything is 2020, LINQ query you are still using expression tree
Let's talk about the locks in the database
Idea - the. IML file was not automatically generated by the project
Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
Adobe Prelude /Pl 2020软件安装包(附安装教程)
More than 50 object detection datasets from different industries
编程界大佬教你:一行Python代码能做出哪些神奇的事情?
IDEA-项目未自动生成 .iml 文件
Animation techniques and details you may not know
The prediction accuracy of the model is as high as 94%! Using machine learning to solve the 200 billion dollar inventory problem perfectly
[C + + learning notes] how about the simple use of the C + + standard library STD:: thread?