LeetCode 335. Self Crossing

挺有趣的一道题,看题解才勉强做出。。。

题意就是给n个长度,然后以上左下右的顺序走。比如 [2,1,1,2]就是先上走2 左走1 下走1 右走2 如果还有就继续向上,向左……

求路径中是否存在交叉。

如果在纸上画一画,就会发现,在保证不出现交叉的前提下,可画的类型无非几种

(实不相瞒,我用微信截图随便画的)

就是说如果出现交叉,并不需要考虑和相离较远的某条线段的交叉,因为肯定会和附近的某条线进行交叉。所以算法是 O(N) 而不是 O(N^2)

总结一下可能出现的交叉 不画了 copy from https://blog.csdn.net/zmq570235977/article/details/51705518

图中考虑的三种情况,实际要考虑多个方向。不过判断条件是一样的。

class Solution {
public:
    bool isSelfCrossing(vector<int>& x) {
        int n = x.size();
        // 小于4条边是不会出现交叉的
        for (int i = 3; i < n; i++) {
            if (
                x[i - 1] <= x[i - 3] && x[i] >= x[i - 2]
                || (i >= 4 && i <= 8
                    && x[i - 4] + x[i] >= x[i - 2]
                    && x[i - 3] == x[i - 1])
                || (i >= 5 && i <= 8
                    && x[i - 1] <= x[i - 3]
                    && x[i - 5] + x[i - 1] >= x[i - 3]
                    && x[i - 4] <= x[i - 2]
                    && x[i - 4] + x[i] >= x[i - 2])
            ) return true;
        }
        return false;
    }
};

原文地址:https://www.cnblogs.com/wenruo/p/12181570.html

时间: 2024-10-18 11:03:46

LeetCode 335. Self Crossing的相关文章

335. Self Crossing

/* * 335. Self Crossing * 2016-7-10 by Mingyang */ // Categorize the self-crossing scenarios, there are 3 of them: // 1. Fourth line crosses first line and works for fifth line crosses second line and so on... // 2. Fifth line meets first line and wo

【LeetCode】Self Crossing(335)

1. Description You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. In other words, after each move you

[LeetCode][JavaScript]Self Crossing

Self Crossing You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south,x[3] metres to the east and so on. In other words, after each move your

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

继续过Hard题目.0209

http://www.cnblogs.com/charlesblc/p/6372971.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

练练脑,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

继续过Hard题目

接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 L

练几道,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

Leetcode: Self Crossing

You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. In other words, after each move your direction cha