【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 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) 2. Answer
public class Solution {
public boolean isSelfCrossing(int[] x) {
// Check for initial four values manually.
if (x.length < 4) {
for (int el : x) {
if (el == 0)
return true;
}
return false;
}
for (int i = 3; i < x.length; i++) {
int cur = x[i];
if (cur == 0)
return true;
// At any point of time, i-1 has to be less than i-3 in order to
// intersect. Draw few figures to realize this.
if (x[i - 1] <= x[i - 3]) {
// Basic case. Straight forward intersection.
// ___
// |___|__
// |
//
if (cur >= x[i - 2]) {
return true;
}
// Special case.
if (i >= 5) {
// if i-2 edge is less than i-4 th edge then it cannot
// intersect no matter what if i < i-2 th edge.
// ____
// | _ |
// |__| |
// |
if (x[i - 2] < x[i - 4])
continue;
// the intersecting case.
// ____
// ___| |
// | | |
// | | |
// |_______|
//
if ((x[i] + x[i - 4] >= x[i - 2])
&& (x[i - 1] + x[i - 5] >= x[i - 3]))
return true;
}
}
// equals case
// ___
// | |
// |___|
//
if (i >= 4)
if (x[i - 1] == x[i - 3] && cur + x[i - 4] == x[i - 2])
return true;
}
return false;
}
}
【LeetCode】Self Crossing(335)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Happy Number(easy)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- 【LeetCode】Counting Bits(338)
1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
随机推荐
- [poi2007]mem
题意:给定点数n<=300000的一棵树,然后初始时每条边权值为1,接下来按照时间点先后顺序的n+m-1个操作, 操作有两种: 1.A a b 把a到b的边权改为0 2.W u 求1号点到u号点 ...
- GDB调试精粹及使用实例(转)
一:列文件清单 1. List (gdb) list line1,line2 二:执行程序 要想运行准备调试的程序,可使用run命令,在它后面可以跟随发给该程序的任何参数,包括标准输入和标准输出说明符 ...
- printf(""); 输出小题目
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h> int main(){ int i=43; printf("%d\n&q ...
- VC++ 19 (VS2015) 编译器系统环境变量配置
Visual C++的cl.exe编译器是微软推出的编译器,干什么的怎么用也不赘述了.大多数情况都是直接在Visual Studio里写代码然后点击"播放"按钮让Visual St ...
- sublime Text 2 制表符
写python的时候经常会有提示indent expected错误很常见,比如同一个文件有的是用空格敲出来的,有的是用tab键出来的这时候错误很难排查,这里说说在notepad++跟sublime下的 ...
- 算法:POJ1008 Maya Calendar
此题非常水,不做说明. package practice; import java.io.BufferedInputStream; import java.util.Scanner; /** * @a ...
- 【WEB】初探Spring MVC框架
Spring MVC框架算是当下比较流行的Java开源框架.但实话实说,做了几年WEB项目,完全没有SpringMVC实战经验,乃至在某些交流场合下被同行严重鄙视“奥特曼”了.“心塞”的同时,只好默默 ...
- [异常解决] ubuntu上安采用sudo启动的firefox,ibus输入法失效问题解决
采用sudo启动的应用是root权限的应用, ibus失效是因为ibus的初始配置采用user权限: 而root下运行的firefox输入法的配置还是停留在默认情况~ 解决方案是在shell下以roo ...
- Azure China (9) 在Azure China配置CDN服务
<Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China Update 2015-11-20:Azure China CDN服 ...
- SQL关联查询总结
employee表: department表: 笛卡儿积: 等值连接: 内连接(和等值连接效果是相同的): 外连接(外连接不仅返回满足条件的记录,还将返回不满足条件的记录)以下是左外连接,右外连接.全 ...