当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- ECMAScript7规范中的instanceof操作符
- ROS learning: remote start ROS node
- Got timeout reading communication packets解决方法
- use Xunit.DependencyInjection Transformation test project
- Cpp(一) 安装CMake
- Hand tearing algorithm - handwritten singleton mode
- 凯撒密码实现
- What do you think of the most controversial programming ideas?
- Go之发送钉钉和邮箱
- LadonGo开源全平台渗透扫描器框架
猜你喜欢

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

Download, installation and configuration of Sogou input method in Ubuntu

构造请求日志分析系统

爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】

计组-总线通信控制之异步串行通信的数据传输

laravel8更新之维护模式改进

See once to understand, graphic single chain table inversion

Do not understand the underlying principle of database index? That's because you don't have a B tree in your heart

看一遍就理解,图解单链表反转

Data structure and sorting algorithm
随机推荐
Adobe Lightroom /Lr 2021软件安装包(附安装教程)
Delphi10's rest.json And system.json Step on the pit
Wechat applet request reported 400 error @ requestbody failed to receive
WPF personal summary on drawing
Speed up your website with jsdelivr
爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】
[original] the influence of arm platform memory and cache on the real-time performance of xenomai
Implementation of multi GPU distributed training with horovod in Amazon sagemaker pipeline mode
Do not understand the underlying principle of database index? That's because you don't have a B tree in your heart
微服务的出现和意义的探索
A detailed explanation of microservice architecture
What details does C + + improve on the basis of C
Using subprocess residue in supervisor and python multiprocessing
What do you think of the most controversial programming ideas?
面部识别:攻击类型和反欺骗技术
On hiz buffer
Recommend suicide, openai warns: gpt-3 is too risky for medical purposes
How Facebook open source framework simplifies pytorch experiment
Download, installation and configuration of Sogou input method in Ubuntu
Python 图片识别 OCR