Skip to content

Commit b43170b

Browse files
refactor 335
1 parent b7cff39 commit b43170b

File tree

1 file changed

+23
-19
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+23
-19
lines changed

src/main/java/com/fishercoder/solutions/_335.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,33 @@ Return true (self crossing)
4545
4646
*/
4747
public class _335 {
48-
49-
/**reference: https://discuss.leetcode.com/topic/38014/java-oms-with-explanation/2*/
50-
public boolean isSelfCrossing(int[] x) {
51-
int l = x.length;
52-
if (l <= 3) {
53-
return false;
54-
}
55-
56-
for (int i = 3; i < l; i++) {
57-
if (x[i] >= x[i - 2] && x[i - 1] <= x[i - 3]) {
58-
return true; //Fourth line crosses first line and onward
48+
public static class Solution1 {
49+
/** reference: https://discuss.leetcode.com/topic/38014/java-oms-with-explanation/2 */
50+
public boolean isSelfCrossing(int[] x) {
51+
int l = x.length;
52+
if (l <= 3) {
53+
return false;
5954
}
60-
if (i >= 4) {
61-
if (x[i - 1] == x[i - 3] && x[i] + x[i - 4] >= x[i - 2]) {
62-
return true; // Fifth line meets first line and onward
55+
56+
for (int i = 3; i < l; i++) {
57+
if (x[i] >= x[i - 2] && x[i - 1] <= x[i - 3]) {
58+
return true; //Fourth line crosses first line and onward
6359
}
64-
}
65-
if (i >= 5) {
66-
if (x[i - 2] - x[i - 4] >= 0 && x[i] >= x[i - 2] - x[i - 4] && x[i - 1] >= x[i - 3] - x[i - 5] && x[i - 1] <= x[i - 3]) {
67-
return true; // Sixth line crosses first line and onward
60+
if (i >= 4) {
61+
if (x[i - 1] == x[i - 3] && x[i] + x[i - 4] >= x[i - 2]) {
62+
return true; // Fifth line meets first line and onward
63+
}
64+
}
65+
if (i >= 5) {
66+
if (x[i - 2] - x[i - 4] >= 0
67+
&& x[i] >= x[i - 2] - x[i - 4]
68+
&& x[i - 1] >= x[i - 3] - x[i - 5]
69+
&& x[i - 1] <= x[i - 3]) {
70+
return true; // Sixth line crosses first line and onward
71+
}
6872
}
6973
}
74+
return false;
7075
}
71-
return false;
7276
}
7377
}

0 commit comments

Comments
 (0)