当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 快速上手Git
- Deep into web workers (1)
- go wire 依赖注入入门
- Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
- 微信小程序request报400错误 @RequestBody接收不到
- Three steps, one pit, five steps and one thunder, how to lead the technical team under the rapid growth?
- The road of cloud computing: a free AWS cloud server
- 【解决方案】分布式定时任务解决方案
- Awk implements SQL like join operation
- Lay UI left tree Dtree right list table
猜你喜欢

数据库基本操作

What do you think of the most controversial programming ideas?

编程界大佬教你:一行Python代码能做出哪些神奇的事情?
![[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

WPF 关于绘图个人总结

团灭 LeetCode 股票买卖问题

密码学-尚硅谷
![A compilation bug brought by vs2015 Update1 update [existing solutions]](/img/3b/00bc81122d330c9d59909994e61027.jpg)
A compilation bug brought by vs2015 Update1 update [existing solutions]

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

微信小程序request报400错误 @RequestBody接收不到
随机推荐
【原创】ARM平台内存和cache对xenomai实时性的影响
What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
Hand tearing algorithm - handwritten singleton mode
凯撒密码实现
Got timeout reading communication packets解决方法
Cpp(二) 创建Cpp工程
Animation techniques and details you may not know
来自不同行业领域的50多个对象检测数据集
Thinkphp6中where条件中字段与字段比较条件的写法
Jingtao project day09
Exploration and practice of growingio responsive programming
栈-括号的匹配
Get started, GIT
Got timeout reading communication packets解决方法
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?
use Xunit.DependencyInjection Transformation test project
ngnix集群高并发
Face recognition: attack types and anti spoofing techniques
awk实现类sql的join操作
CPP (4) boost installation and basic use for Mac