当前位置:网站首页>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安全(四)---XSS攻击
- 年薪90万程序员不如月入3800公务员?安稳与高收入,到底如何选择?
- 编程界大佬教你:一行Python代码能做出哪些神奇的事情?
- 洞察——风格注意力网络(SANet)在任意风格迁移中的应用
- Data structure and sorting algorithm
- How to deal with data leakage and deletion related to business life and death?
- Adobe media encoder / me 2021 software installation package (with installation tutorial)
- 微服务的出现和意义的探索
- Kylin on kubernetes' practice on eBay
- 爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】
猜你喜欢

WPF 关于绘图个人总结

Lay UI left tree Dtree right list table

Insight -- the application of sanet in arbitrary style transfer

delphi10的rest.json与system.json的踩坑

团灭 LeetCode 股票买卖问题

Code Review Best Practices

一次公交卡被“盗刷”事件带来的思考

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

Cryptography - Shangsi Valley

关于晋升全栈工程师,从入门到放弃的神功秘籍,不点进来看一看?
随机推荐
On the stock trading of leetcode
Adobe media encoder / me 2021 software installation package (with installation tutorial)
Python image recognition OCR
websocket+probuf.原理篇
虚拟DOM中给同一层级的元素设置固定且唯一的key为什么能提高性能
Search and replace of sed
CPP (4) boost installation and basic use for Mac
微信小程序request报400错误 @RequestBody接收不到
栈-括号的匹配
手撕算法-手写单例模式
On the coverage technology and best practice of go code
Ladongo open source full platform penetration scanner framework
Principles of websocket + probuf
Delphi10's rest.json And system.json Step on the pit
Cpp(一) 安装CMake
来自不同行业领域的50多个对象检测数据集
使用jsDelivr加速你的网站
Cpp(三) 什么是CMake
Using pipe() to improve code readability in pandas
static+代码块+多态+异常