当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Thinkphp6中where条件中字段与字段比较条件的写法
- C++在C的基础上改进了哪些细节
- Web Security (1) -- browser homology strategy
- Sentry installation
- ROS learning: remote start ROS node
- ngnix集群高并发
- Improvement of maintenance mode of laravel8 update
- Assembly function MCALL systemstack asmcgocal system call
- Got timeout reading communication packets解决方法
- AFO
猜你喜欢
Lay UI left tree Dtree right list table
Deep into web workers (1)
Animation techniques and details you may not know
Let's talk about the locks in the database
delphi10的rest.json与system.json的踩坑
A detailed explanation of microservice architecture
Improvement of maintenance mode of laravel8 update
What do you think of the most controversial programming ideas?
【原创】ARM平台内存和cache对xenomai实时性的影响
Got timeout reading communication packets解决方法
随机推荐
来自不同行业领域的50多个对象检测数据集
Animation techniques and details you may not know
Sentry 安装
sed之查找替换
Web Security (3) -- CSRF attack
CPP (2) creating CPP project
Let's talk about the locks in the database
面部识别:攻击类型和反欺骗技术
A detailed explanation of microservice architecture
Web安全(一)---浏览器同源策略
Stack bracket matching
How Facebook open source framework simplifies pytorch experiment
Data transmission of asynchronous serial communication controlled by group bus communication
Practice of Xiaoxiong school development board: real equipment access of smart street lamp sandbox experiment
Design pattern of facade and mediator
栈-括号的匹配
[C + + learning notes] how about the simple use of the C + + standard library STD:: thread?
Static + code block + polymorphism + exception
Sentry installation
Wechat applet request reported 400 error @ requestbody failed to receive