当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Share several vs Code plug-ins I use everyday
- Web Security (1) -- browser homology strategy
- Insight -- the application of sanet in arbitrary style transfer
- websocket+probuf.原理篇
- 什么都2020了,LINQ查询你还在用表达式树
- A detailed explanation of microservice architecture
- On hiz buffer
- 密码学-尚硅谷
- What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
- WPF personal summary on drawing
猜你喜欢

More than 50 object detection datasets from different industries

Analysis of kubernetes service types: from concept to practice

【C++学习笔记】C++ 标准库 std::thread 的简单使用,一文搞定还不简单?

Deep into web workers (1)

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

Got timeout reading communication packets解决方法

团灭 LeetCode 股票买卖问题

Insight -- the application of sanet in arbitrary style transfer

微服务的出现和意义的探索

Animation techniques and details you may not know
随机推荐
Dynamic programming -- state compression DP of set represented by binary
The prediction accuracy of the model is as high as 94%! Using machine learning to solve the 200 billion dollar inventory problem perfectly
编程界大佬教你:一行Python代码能做出哪些神奇的事情?
It's time to end bertology
Static + code block + polymorphism + exception
Improvement of maintenance mode of laravel8 update
Go sending pin and email
CPP (2) creating CPP project
Search and replace of sed
Reflection on a case of bus card being stolen and swiped
How to choose a good company
手撕算法-手写单例模式
Improvement of maintenance mode of laravel8 update
你可能不知道的Animation动画技巧与细节
What details does C + + improve on the basis of C
计组-总线通信控制之异步串行通信的数据传输
Code Review Best Practices
工作1-3年的程序员,应该具备怎么样的技术能力?该如何提升?
Supervisor process management installation and use
laravel8更新之维护模式改进