当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Web安全(二)---跨域资源共享
- On the coverage technology and best practice of go code
- ngnix集群高并发
- Supervisor process management installation and use
- Adobe media encoder / me 2021 software installation package (with installation tutorial)
- 微服务的出现和意义的探索
- What magic things can a line of Python code do?
- 团灭 LeetCode 股票买卖问题
- Sentry 安装
- Awk implements SQL like join operation
猜你喜欢
android基础-RadioButton(单选按钮)
IDEA-项目未自动生成 .iml 文件
What magic things can a line of Python code do?
工作1-3年的程序员,应该具备怎么样的技术能力?该如何提升?
【解决方案】分布式定时任务解决方案
Data transmission of asynchronous serial communication controlled by group bus communication
use Xunit.DependencyInjection Transformation test project
On the coverage technology and best practice of go code
Lay UI left tree Dtree right list table
看一遍就理解,图解单链表反转
随机推荐
More than 50 object detection datasets from different industries
Using subprocess residue in supervisor and python multiprocessing
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
Download, installation and configuration of Sogou input method in Ubuntu
Awk implements SQL like join operation
微服务的出现和意义的探索
Cpp(二) 创建Cpp工程
supervisor和Python多进程multiprocessing使用 子进程残留问题
你可能不知道的Animation动画技巧与细节
Cryptography - Shangsi Valley
What is the relationship between low code vs model driven?
How to deal with data leakage and deletion related to business life and death?
Ubuntu下搜狗输入法的下载安装及配置
ngnix集群高并发
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
How did I lose control of the team?
Got timeout reading communication packets解决方法
Animation techniques and details you may not know
On the concurrency of update operation
Data transmission of asynchronous serial communication controlled by group bus communication