【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 ...
随机推荐
- PeerConnection
Example(摘) /*When two peers decide they are going to set up a connection to each other, they both go ...
- Win10 设置外网多用户远程桌面连接
主要原理:利用路由器的虚拟服务器功能,将内网的Ip地址通过端口映射提供给外网,使得外网能够访问到目的主机. 1. 配置路由器上的虚拟服务器,假设目的主机内网的ip为192.168.1.100,则配置如 ...
- linux配置本地源
yum本地源配置 标签: centosplugins网络file虚拟机linux 2011-10-09 21:40 12093人阅读 评论(1) 收藏 举报 分类: linux yum 在网上找了很 ...
- EI Index
Journal of Software (JoS). China. [link]
- cocoapods的时候出现的问题 _OBJC_CLASS_$_XXX
最新的cocoapod导入xmpp的时候,会出现循环依赖,所以撸主选择了手动导入. 一开始还用的挺开心的,后来,使用cocoapods导入其他的框架,发现调用的时候总是报错. Undefined sy ...
- SQLServer内核架构剖析 (转载)
SQL Server内核架构剖析 (转载) 这篇文章在我电脑里好长时间了,今天不小心给翻出来了,觉得写得很不错,因此贴出来共享. 不得不承认的是,一个优秀的软件是一步一步脚踏实地积累起来的,众多优秀的 ...
- 初试TinyIoCContainer笔记
第一次看到TinyIoCContainer是在用NancyFx的时候,在Bootstrapper那里看到了她的影子. 那些叫Tiny的东西都挺有意思,IoC容器是我第一次遇到,于是找了些文章看了看,自 ...
- 写了一个简单的NodeJS实现的进程间通信的例子
1. cluster介绍 大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,有人开发了第三方的cluster,让node可以利用多核CPU ...
- Ubuntu Desktop开发生产环境搭建
Ubuntu Desktop开发生产环境搭建 1 开发生产环境搭建 在本节内容开始前,先定义一下使用场合,没有哪种系统或者设备是万能的,都有它的优点和缺点,能够在具体的使用场景,根据自身的需求来取 ...
- 本地DNS安装
在centOS里最常用的DNS服务工具应该是bind了.下面就以bind为例做一个DNS服务. 首先查看bind 是否已经安装 Rpm -qa | gerp bind 如果没有的话就用yum 安装一下 ...