Skip to content

Commit e29eaab

Browse files
committed
+ problem 335
1 parent 4e08d56 commit e29eaab

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 335. Self Crossing
2+
You are given an array of integers `distance`.
3+
4+
You start at the point `(0, 0)` on an **X-Y plane**, and you move `distance[0]` meters to the north, then `distance[1]` meters to the west, `distance[2]` meters to the south, `distance[3]` meters to the east, and so on. In other words, after each move, your direction changes counter-clockwise.
5+
6+
Return `true` *if your path crosses itself or* `false` *if it does not*.
7+
8+
#### Example 1:
9+
![](https://assets.leetcode.com/uploads/2022/12/21/11.jpg)
10+
<pre>
11+
<strong>Input:</strong> distance = [2,1,1,2]
12+
<strong>Output:</strong> true
13+
<strong>Explanation:</strong> The path crosses itself at the point (0, 1).
14+
</pre>
15+
16+
#### Example 2:
17+
![](https://assets.leetcode.com/uploads/2022/12/21/22.jpg)
18+
<pre>
19+
<strong>Input:</strong> distance = [1,2,3,4]
20+
<strong>Output:</strong> false
21+
<strong>Explanation:</strong> The path does not cross itself at any point.
22+
</pre>
23+
24+
#### Example 3:
25+
![](https://assets.leetcode.com/uploads/2022/12/21/33.jpg)
26+
<pre>
27+
<strong>Input:</strong> distance = [1,1,1,2,1]
28+
<strong>Output:</strong> true
29+
<strong>Explanation:</strong> The path crosses itself at the point (0, 0).
30+
</pre>
31+
32+
#### Constraints:
33+
* <code>1 <= distance.length <= 10<sup>5</sup></code>
34+
* <code>1 <= distance[i] <= 10<sup>5</sup></code>
35+
36+
## Solutions (Rust)
37+
38+
### 1. Solution
39+
```Rust
40+
impl Solution {
41+
pub fn is_self_crossing(mut distance: Vec<i32>) -> bool {
42+
let mut i = 2;
43+
44+
while i < distance.len() {
45+
if distance[i] <= distance[i - 2] {
46+
let mut tmp = distance[i - 2];
47+
if i > 3 {
48+
tmp -= distance[i - 4];
49+
}
50+
if i > 2 && distance[i] >= tmp {
51+
distance[i - 1] -= distance[i - 3];
52+
}
53+
break;
54+
}
55+
56+
i += 1;
57+
}
58+
59+
while i < distance.len() - 1 {
60+
i += 1;
61+
62+
if distance[i] >= distance[i - 2] {
63+
return true;
64+
}
65+
}
66+
67+
false
68+
}
69+
}
70+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 335. 路径交叉
2+
给你一个整数数组 `distance`
3+
4+
**X-Y** 平面上的点 `(0,0)` 开始,先向北移动 `distance[0]` 米,然后向西移动 `distance[1]` 米,向南移动 `distance[2]` 米,向东移动 `distance[3]` 米,持续移动。也就是说,每次移动后你的方位会发生逆时针变化。
5+
6+
判断你所经过的路径是否相交。如果相交,返回 `true` ;否则,返回 `false`
7+
8+
#### 示例 1:
9+
![](https://assets.leetcode.com/uploads/2021/03/14/selfcross1-plane.jpg)
10+
<pre>
11+
<strong>输入:</strong> distance = [2,1,1,2]
12+
<strong>输出:</strong> true
13+
</pre>
14+
15+
#### 示例 2:
16+
![](https://assets.leetcode.com/uploads/2021/03/14/selfcross2-plane.jpg)
17+
<pre>
18+
<strong>输入:</strong> distance = [1,2,3,4]
19+
<strong>输出:</strong> false
20+
</pre>
21+
22+
#### 示例 3:
23+
![](https://assets.leetcode.com/uploads/2021/03/14/selfcross3-plane.jpg)
24+
<pre>
25+
<strong>输入:</strong> distance = [1,1,1,1]
26+
<strong>输出:</strong> true
27+
</pre>
28+
29+
#### 提示:
30+
* <code>1 <= distance.length <= 10<sup>5</sup></code>
31+
* <code>1 <= distance[i] <= 10<sup>5</sup></code>
32+
33+
## 题解 (Rust)
34+
35+
### 1. 题解
36+
```Rust
37+
impl Solution {
38+
pub fn is_self_crossing(mut distance: Vec<i32>) -> bool {
39+
let mut i = 2;
40+
41+
while i < distance.len() {
42+
if distance[i] <= distance[i - 2] {
43+
let mut tmp = distance[i - 2];
44+
if i > 3 {
45+
tmp -= distance[i - 4];
46+
}
47+
if i > 2 && distance[i] >= tmp {
48+
distance[i - 1] -= distance[i - 3];
49+
}
50+
break;
51+
}
52+
53+
i += 1;
54+
}
55+
56+
while i < distance.len() - 1 {
57+
i += 1;
58+
59+
if distance[i] >= distance[i - 2] {
60+
return true;
61+
}
62+
}
63+
64+
false
65+
}
66+
}
67+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn is_self_crossing(mut distance: Vec<i32>) -> bool {
3+
let mut i = 2;
4+
5+
while i < distance.len() {
6+
if distance[i] <= distance[i - 2] {
7+
let mut tmp = distance[i - 2];
8+
if i > 3 {
9+
tmp -= distance[i - 4];
10+
}
11+
if i > 2 && distance[i] >= tmp {
12+
distance[i - 1] -= distance[i - 3];
13+
}
14+
break;
15+
}
16+
17+
i += 1;
18+
}
19+
20+
while i < distance.len() - 1 {
21+
i += 1;
22+
23+
if distance[i] >= distance[i - 2] {
24+
return true;
25+
}
26+
}
27+
28+
false
29+
}
30+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
[331][331l] |[Verify Preorder Serialization of a Binary Tree][331] |![rs]
260260
[332][332l] |[Reconstruct Itinerary][332] |![py]
261261
[334][334l] |[Increasing Triplet Subsequence][334] |![rb]&nbsp;&nbsp;![rs]
262+
[335][335l] |[Self Crossing][335] |![rs]
262263
[337][337l] |[House Robber III][337] |![py]&nbsp;&nbsp;![rb]
263264
[338][338l] |[Counting Bits][338] |![rs]
264265
[341][341l] |[Flatten Nested List Iterator][341] |![rs]
@@ -2181,6 +2182,7 @@
21812182
[331]:Problemset/0331-Verify%20Preorder%20Serialization%20of%20a%20Binary%20Tree/README.md#331-verify-preorder-serialization-of-a-binary-tree
21822183
[332]:Problemset/0332-Reconstruct%20Itinerary/README.md#332-reconstruct-itinerary
21832184
[334]:Problemset/0334-Increasing%20Triplet%20Subsequence/README.md#334-increasing-triplet-subsequence
2185+
[335]:Problemset/0335-Self%20Crossing/README.md#335-self-crossing
21842186
[337]:Problemset/0337-House%20Robber%20III/README.md#337-house-robber-iii
21852187
[338]:Problemset/0338-Counting%20Bits/README.md#338-counting-bits
21862188
[341]:Problemset/0341-Flatten%20Nested%20List%20Iterator/README.md#341-flatten-nested-list-iterator
@@ -4096,6 +4098,7 @@
40964098
[331l]:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/
40974099
[332l]:https://leetcode.com/problems/reconstruct-itinerary/
40984100
[334l]:https://leetcode.com/problems/increasing-triplet-subsequence/
4101+
[335l]:https://leetcode.com/problems/self-crossing/
40994102
[337l]:https://leetcode.com/problems/house-robber-iii/
41004103
[338l]:https://leetcode.com/problems/counting-bits/
41014104
[341l]:https://leetcode.com/problems/flatten-nested-list-iterator/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
[331][331l] |[验证二叉树的前序序列化][331] |![rs]
260260
[332][332l] |[重新安排行程][332] |![py]
261261
[334][334l] |[递增的三元子序列][334] |![rb]&nbsp;&nbsp;![rs]
262+
[335][335l] |[路径交叉][335] |![rs]
262263
[337][337l] |[打家劫舍 III][337] |![py]&nbsp;&nbsp;![rb]
263264
[338][338l] |[比特位计数][338] |![rs]
264265
[341][341l] |[扁平化嵌套列表迭代器][341] |![rs]
@@ -2181,6 +2182,7 @@
21812182
[331]:Problemset/0331-Verify%20Preorder%20Serialization%20of%20a%20Binary%20Tree/README_CN.md#331-验证二叉树的前序序列化
21822183
[332]:Problemset/0332-Reconstruct%20Itinerary/README_CN.md#332-重新安排行程
21832184
[334]:Problemset/0334-Increasing%20Triplet%20Subsequence/README_CN.md#334-递增的三元子序列
2185+
[335]:Problemset/0335-Self%20Crossing/README_CN.md#335-路径交叉
21842186
[337]:Problemset/0337-House%20Robber%20III/README_CN.md#337-打家劫舍-iii
21852187
[338]:Problemset/0338-Counting%20Bits/README_CN.md#338-比特位计数
21862188
[341]:Problemset/0341-Flatten%20Nested%20List%20Iterator/README_CN.md#341-扁平化嵌套列表迭代器
@@ -4096,6 +4098,7 @@
40964098
[331l]:https://leetcode.cn/problems/verify-preorder-serialization-of-a-binary-tree/
40974099
[332l]:https://leetcode.cn/problems/reconstruct-itinerary/
40984100
[334l]:https://leetcode.cn/problems/increasing-triplet-subsequence/
4101+
[335l]:https://leetcode.cn/problems/self-crossing/
40994102
[337l]:https://leetcode.cn/problems/house-robber-iii/
41004103
[338l]:https://leetcode.cn/problems/counting-bits/
41014104
[341l]:https://leetcode.cn/problems/flatten-nested-list-iterator/

0 commit comments

Comments
 (0)