当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Getting started with go wire dependency injection
- 京淘项目day09
- Let's talk about the locks in the database
- Download, installation and configuration of Sogou input method in Ubuntu
- Idea - the. IML file was not automatically generated by the project
- Web Security (4) -- XSS attack
- Face recognition: attack types and anti spoofing techniques
- Cpp(四) Boost安装及基本使用 for Mac
- ECMAScript7规范中的instanceof操作符
- Principles of websocket + probuf
猜你喜欢
京淘项目day09
Ubuntu下搜狗输入法的下载安装及配置
Let's talk about the locks in the database
More than 50 object detection datasets from different industries
Git代码提交操作,以及git push提示failed to push some refs'XXX'
The road of cloud computing: a free AWS cloud server
计组-总线通信控制之异步串行通信的数据传输
WPF 关于绘图个人总结
手撕算法-手写单例模式
Go sending pin and email
随机推荐
Awk implements SQL like join operation
CPP (2) creating CPP project
Go之发送钉钉和邮箱
Writing method of field and field comparison condition in where condition in thinkphpp6
编程界大佬教你:一行Python代码能做出哪些神奇的事情?
年薪90万程序员不如月入3800公务员?安稳与高收入,到底如何选择?
Get started, GIT
ngnix集群高并发
Python image recognition OCR
[C + + learning notes] how about the simple use of the C + + standard library STD:: thread?
Data structure and sorting algorithm
What do you think of the most controversial programming ideas?
[solution] distributed timing task solution
Cryptography - Shangsi Valley
Practice of Xiaoxiong school development board: real equipment access of smart street lamp sandbox experiment
密码学-尚硅谷
手撕算法-手写单例模式
Thinkphp6中where条件中字段与字段比较条件的写法
CPP (1) installation of cmake
The road of cloud computing: a free AWS cloud server