当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 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?
- What details does C + + improve on the basis of C
- How to deal with data leakage and deletion related to business life and death?
- static+代码块+多态+异常
- Sentry 安装
- Face recognition: attack types and anti spoofing techniques
- How to choose a good company
- Speed up your website with jsdelivr
- Idea - the. IML file was not automatically generated by the project
- Static + code block + polymorphism + exception
猜你喜欢

京淘项目day09

Deep into web workers (1)

Go之发送钉钉和邮箱

Lay UI left tree Dtree right list table

How to learn technology efficiently

The road of cloud computing: a free AWS cloud server

What do you think of the most controversial programming ideas?

微服务的出现和意义的探索

Git代码提交操作,以及git push提示failed to push some refs'XXX'

Ladongo open source full platform penetration scanner framework
随机推荐
ROS learning: remote start ROS node
Animation techniques and details you may not know
京淘项目day09
CPP (2) creating CPP project
A compilation bug brought by vs2015 Update1 update [existing solutions]
Face recognition: attack types and anti spoofing techniques
High concurrency in ngnix cluster
Get tree menu list
delphi10的rest.json与system.json的踩坑
凯撒密码实现
Lay UI left tree Dtree right list table
手撕算法-手写单例模式
关于update操作并发问题
CPP (4) boost installation and basic use for Mac
Git code submission operation, and git push prompt failed to push some refs'xxx '
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?
Android Basics - RadioButton (radio button)
On hiz buffer
There's not much time left for Kwai Chung.
C++在C的基础上改进了哪些细节