[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 direction changes counter-clockwise.

Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not.

Example 1:

Given x = [2, 1, 1, 2],
Return true (self crossing)

Example 2:

Given x = [1, 2, 3, 4],
Return false (not self crossing)

Example 3:

Given x = [1, 1, 1, 1],
Return true (self crossing)

https://leetcode.com/problems/self-crossing/



逆时针画线,求线是否有交叉。

看图,三种情况,对应了代码中的注释。

 1 /**
 2  * @param {number[]} x
 3  * @return {boolean}
 4  */
 5 var isSelfCrossing = function(x) {
 6     for(var i = 3; i < x.length; i++){
 7         //i. [2, 1, 1, 2]
 8         if(x[i - 3] && x[i] >= x[i - 2] && x[i - 1] <= x[i - 3])
 9             return true;
10         //ii. [1, 1, 2, 1, 1]
11         if(x[i - 4] && x[i - 3] === x[i - 1] && x[i - 4] + x[i] >= x[i - 2])
12             return true;
13         //iii. [1, 1, 2, 2, 1, 1]
14         if(x[i - 5] && x[i - 4] <= x[i -2] && x[i] + x[i - 4] >= x[i -2]
15             && x[i - 3] >= x[i - 1] && x[i - 5] + x[i - 1] >= x[ i - 3])
16             return true;
17     }
18     return false;
19 };
时间: 2024-10-19 05:21:35

[LeetCode][JavaScript]Self Crossing的相关文章

[LeetCode][JavaScript]Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

[LeetCode][JavaScript]Insert Interval

https://leetcode.com/problems/insert-interval/ Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start t

[LeetCode][JavaScript]Maximum Subarray

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. https://leetcod

[LeetCode][JavaScript]Coin Change

Coin Change You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination o

[LeetCode][JavaScript]Remove Duplicate Letters

Remove Duplicate Letters Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results

[LeetCode][JavaScript]Additive Number

Additive Number Additive number is a positive integer whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum o

[LeetCode][JavaScript]Unique Binary Search Trees II

Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1

[LeetCode][JavaScript]Binary Tree Preorder Traversal

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? https://leetcod

[LeetCode][JavaScript]Trapping Rain Water

Trapping Rain Water Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map