当前位置:网站首页>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 learning: remote start ROS node
- A compilation bug brought by vs2015 Update1 update [existing solutions]
- Go sending pin and email
- 京淘项目day09
- Animation techniques and details you may not know
- Three steps, one pit, five steps and one thunder, how to lead the technical team under the rapid growth?
- delphi10的rest.json与system.json的踩坑
- A detailed explanation of microservice architecture
- 年薪90万程序员不如月入3800公务员?安稳与高收入,到底如何选择?
- Insight -- the application of sanet in arbitrary style transfer
猜你喜欢
构造请求日志分析系统
Hand tearing algorithm - handwritten singleton mode
WPF personal summary on drawing
使用 Xunit.DependencyInjection 改造测试项目
Code Review Best Practices
Jingtao project day09
Face recognition: attack types and anti spoofing techniques
Go sending pin and email
What magic things can a line of Python code do?
爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】
随机推荐
Adobe Prelude / PL 2020 software installation package (with installation tutorial)
Web安全(三)---CSRF攻击
手撕算法-手写单例模式
工作1-3年的程序员,应该具备怎么样的技术能力?该如何提升?
状态压缩:对动态规划进行降维打击
Principles of websocket + probuf
supervisor进程管理安装使用
凯撒密码实现
Exploration and practice of growingio responsive programming
What details does C + + improve on the basis of C
CPP (1) installation of cmake
Go之发送钉钉和邮箱
Static + code block + polymorphism + exception
ROS learning: remote start ROS node
Delphi10's rest.json And system.json Step on the pit
Python image recognition OCR
Cpp(三) 什么是CMake
Insight -- the application of sanet in arbitrary style transfer
easyui dialog“缓存问题”
Sentry installation