当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】
- What is the relationship between low code vs model driven?
- 手撕算法-手写单例模式
- Adobe Lightroom /Lr 2021软件安装包(附安装教程)
- 使用 Xunit.DependencyInjection 改造测试项目
- awk实现类sql的join操作
- Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
- Download, installation and configuration of Sogou input method in Ubuntu
- Jingtao project day09
- A detailed explanation of microservice architecture
猜你喜欢
Basic operation of database
Jingtao project day09
The road of cloud computing: a free AWS cloud server
获取树形菜单列表
看一遍就理解,图解单链表反转
LadonGo开源全平台渗透扫描器框架
android基础-RadioButton(单选按钮)
Insight -- the application of sanet in arbitrary style transfer
Insight -- the application of sanet in arbitrary style transfer
关于晋升全栈工程师,从入门到放弃的神功秘籍,不点进来看一看?
随机推荐
Get tree menu list
CPP (3) what is cmake
构造请求日志分析系统
Adobe Prelude /Pl 2020软件安装包(附安装教程)
On the concurrency of update operation
C++在C的基础上改进了哪些细节
关于update操作并发问题
Face recognition: attack types and anti spoofing techniques
Improvement of maintenance mode of laravel8 update
Git代码提交操作,以及git push提示failed to push some refs'XXX'
密码学-尚硅谷
Awk implements SQL like join operation
go wire 依赖注入入门
虚拟DOM中给同一层级的元素设置固定且唯一的key为什么能提高性能
CPP (4) boost installation and basic use for Mac
LadonGo开源全平台渗透扫描器框架
Awk implements SQL like join operation
关于晋升全栈工程师,从入门到放弃的神功秘籍,不点进来看一看?
[C + + learning notes] how about the simple use of the C + + standard library STD:: thread?
Insight -- the application of sanet in arbitrary style transfer