当前位置:网站首页>Judging whether paths intersect or not by leetcode
Judging whether paths intersect or not by leetcode
2020-11-07 21:43:00 【go4it】
order
This article mainly records leetcode Whether the paths intersect
subject
Give you a string path, among path[i] The value of can be 'N'、'S'、'E' perhaps 'W', To the north 、 Southward 、 To the east 、 Move one unit West .
The robot starts from the origin on the two-dimensional plane (0, 0) Starting from , Press path The path indicated to walk .
If paths intersect at any point , That is to go to the position that has been passed before , Please return True ; otherwise , return False .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/path-crossing
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Answer key
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;
}
}
Summary
Here to maintain the past point , Then traverse path The characters of , Yes x,y The coordinates move accordingly , After each move, judge whether the point has passed , Walk past and return true, If not, record the change points in the past points , After traversing, it will return if it does not meet the conditions false.
doc
版权声明
本文为[go4it]所创,转载请带上原文链接,感谢
边栏推荐
- ROS学习---远程启动ROS节点
- 关于update操作并发问题
- 年薪90万程序员不如月入3800公务员?安稳与高收入,到底如何选择?
- How Facebook open source framework simplifies pytorch experiment
- IDEA-项目未自动生成 .iml 文件
- supervisor和Python多进程multiprocessing使用 子进程残留问题
- Thinkphp6中where条件中字段与字段比较条件的写法
- 看一遍就理解,图解单链表反转
- [original] the influence of arm platform memory and cache on the real-time performance of xenomai
- 数据库基本操作
猜你喜欢

Data transmission of asynchronous serial communication controlled by group bus communication

构造请求日志分析系统

一次公交卡被“盗刷”事件带来的思考

use Xunit.DependencyInjection Transformation test project

云计算之路-出海记:整一台 aws 免费云服务器

What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?

编程界大佬教你:一行Python代码能做出哪些神奇的事情?

More than 50 object detection datasets from different industries

Android Basics - RadioButton (radio button)
![[original] the influence of arm platform memory and cache on the real-time performance of xenomai](/img/cb/0395507ece572556b2bab373f013a5.jpg)
[original] the influence of arm platform memory and cache on the real-time performance of xenomai
随机推荐
Supervisor process management installation and use
In the age of screen reading, we suffer from attention deficit syndrome
LadonGo开源全平台渗透扫描器框架
工作1-3年的程序员,应该具备怎么样的技术能力?该如何提升?
Share several vs Code plug-ins I use everyday
A compilation bug brought by vs2015 Update1 update [existing solutions]
Delphi10's rest.json And system.json Step on the pit
Awk implements SQL like join operation
构造请求日志分析系统
一次公交卡被“盗刷”事件带来的思考
easyui dialog“缓存问题”
微信小程序request报400错误 @RequestBody接收不到
Web安全(四)---XSS攻击
Design pattern of facade and mediator
Count the frequency of letters in text (case insensitive)
Sentry 安装
Get started, GIT
Everything is 2020, LINQ query you are still using expression tree
[C + + learning notes] how about the simple use of the C + + standard library STD:: thread?
About the promotion of the whole stack of engineers, from the introduction to give up the secret arts, do not click in to have a look?