首页 > 其他分享 >leetcode-1496-easy

leetcode-1496-easy

时间:2023-03-11 13:44:27浏览次数:37  
标签:key set false easy any 1496 path true leetcode

Path Crossing

Given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane and walk on the path specified by path.

Return true if the path crosses itself at any point, that is, if at any time you are on a location you have previously visited. Return false otherwise.

Example 1:


Input: path = "NES"
Output: false 
Explanation: Notice that the path doesn't cross any point more than once.
Example 2:


Input: path = "NESWW"
Output: true
Explanation: Notice that the path visits the origin twice.
Constraints:

1 <= path.length <= 104
path[i] is either 'N', 'S', 'E', or 'W'.

思路一:模拟操作,用 set 存储坐标点

    public boolean isPathCrossing(String path) {
        char[] chars = path.toCharArray();
        
        int x = 0;
        int y = 0;
        Set<String> set = new HashSet<>();
        set.add("0 0");
        for (char c : chars) {

            if (c == 'N') {
                y++;
            } else if (c == 'S') {
                y--;
            } else if (c == 'W') {
                x--;
            } else {
                x++;
            } 
            
            String key = x + " " + y;
            if (set.contains(key)) {
                return true;
            }

            set.add(key);
        }

        return false;
    }

标签:key,set,false,easy,any,1496,path,true,leetcode
From: https://www.cnblogs.com/iyiluo/p/17205784.html

相关文章

  • leetcode-1523-easy
    CountOddNumbersinanIntervalRangeGiventwonon-negativeintegerslowandhigh.Returnthecountofoddnumbersbetweenlowandhigh(inclusive).Example......
  • LeetCode|2457. 美丽整数的最小增量
    题目链接:2457.美丽整数的最小增量给你两个正整数n和target。如果某个整数每一位上的数字相加小于或等于target,则认为这个整数是一个美丽整数。找出并返回满......
  • 【LeetCode回溯算法#07】子集问题I+II,巩固解题模板并详解回溯算法中的去重问题
    子集力扣题目链接给你一个整数数组nums,数组中的元素互不相同。返回该数组所有可能的子集(幂集)。解集不能包含重复的子集。你可以按任意顺序返回解集。示例1:输......
  • eNSP的使用4  NAT网络地址转换 easyip
    静态转换:一对一绑定 (双向通信)路由器靠地址区分内部主机easy ip :多对一  (单向通信)路由器靠端口(临时端口)区分内部主机easy ip的配置:1.配置aclacl  2000rule perm......
  • easyexcel填坑-校验表头为空,或者不符合预期
    背景:easyexcelv3.1.5实体类已经使用注解@ExcelProperty标注需要导入的属性正文开始关闭忽略空行,防止第一行是空跳过校验 ignoreEmptyRow(false)。此处如果未关闭,第......
  • Leetcode24. 两两交换链表中的节点
    题目描述:给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。 思路:使用虚拟头结点,这样会......
  • 【LeetCode回溯算法#06】复原IP地址详解(练习如何处理边界条件,判断IP合法性)
    复原IP地址力扣题目链接(opensnewwindow)给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式。有效的IP地址正好由四个整数(每个整数位于0到255之......
  • LeetCode306 累加数
    题目描述累加数是一个字符串,组成它的数字可以形成累加序列。一个有效的累加序列必须至少包含3个数。除了最开始的两个数以外,序列中的每个后续数字必须是它之前......
  • LeetCode|1410. HTML 实体解析器
    题目链接:1410.HTML实体解析器「HTML实体解析器」是一种特殊的解析器,它将HTML代码作为输入,并用字符本身替换掉所有这些特殊的字符实体。HTML里这些特殊字符和它......
  • LeetCode206. 反转链表
    题目描述:给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。示例1:输入:head=[1,2,3,4,5]输出:[5,4,3,2,1]示例2:输入:head=[1,2]输出:[2,1]......