Leetcode: 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)
4th line may cross with 1st line, and so on: 5th with 2nd, ...etc
5th line may cross with 1st line, and so on: 6th with 2nd, ...etc
6th line also may cross with 1st line, and so on: 7th with 2nd, ...etc
However, if 7th line also cross with 1st line, either of the following cases should definitely happens:
a. 7th line cross with 2nd line
b. 6th line cross with 1st line
we have covered these cases.
public class Solution {
public boolean isSelfCrossing(int[] x) {
if (x.length <= 3) return false;
for (int i=3; i<x.length; i++) {
//check if 4th line cross with the first line and so on
if (x[i]>=x[i-2] && x[i-1]<=x[i-3]) return true; //check if 5th line cross with the first line and so on
if (i >= 4) {
if (x[i-1]==x[i-3] && x[i]+x[i-4]>=x[i-2]) return true;
} //check if 6th line cross with the first line and so on
if (i >= 5) {
if (x[i-2]>=x[i-4] && x[i]>=x[i-2]-x[i-4] && x[i-1]<=x[i-3] && x[i-1]>=x[i-3]-x[i-5]) return true;
}
}
return false;
}
}
Leetcode: Self Crossing的更多相关文章
- [LeetCode] Self Crossing 自交
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...
- 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)
传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...
- 【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[ ...
- 【LeetCode】335. Self Crossing(python)
Problem:You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metr ...
- [leetcode]335. Self Crossing
You are given an array x of n positive numbers. You start at point (,) and moves x[] metres to the n ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- Leetcode: Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- apache安装 mod_evasive
centos环境下安装 首先安装httpd-devel yum安装mod_evasive 配置mod_evasive: DOSHashTableSize 3097 #哈希表大小(无需修改) ...
- php thread
1-include('w_fun.php');页面已经在执行,则修改include中的函数,倒置旧页面不受影响:新页面生效:2-time 轮流 echo ' <script> windo ...
- 【转】NGUI创建UIRoot后报NullReferenceException的解决办法
本文参考自 http://forum.china.unity3d.com/thread-1099-1-1.html 使用NGUI版本3.7.5. 在创建了一个UIRoot后,有时会报NullRefer ...
- 3D模型修改
xnalara模型修改---增添(技术交流贴2) 其实很早就想做这个教程(流程)但有一种叫拖延症的东东捆了我半年~~于是这个帖子诞生与此,,希望对某些骚年有用... 送TA礼物 回复 举报|1 ...
- 新建android项目报错,代码中找不到错误
通过网上资料的引导,做以下操作: 1.进入C:\Documents and Settings\Administrator\.android 删除路径下的debug.keystore及 ddms.cfg ...
- Python学习笔记——1——基础知识
1.1.变量和算法 python语言很类似人类语言,变量不需要定义类型.比如: 整型 字符串类型 数组 Java int a=12 String s="test" String[] ...
- 在VC6.0中编译头文件时产生moc文件
1.在FileView视图中 右键点击需要产生moc文件的头文件(就是类中包含Q_OBJECT宏,如果没有这个宏就不需要产生moc文件) 2.在右键菜单中选择Setting... 3.选择Custom ...
- php--yii2.0的安装
1.php.ini中去掉php_openssl.dll前面的“;” 2.注意phpstudy中php版本使用5.4n 3.环境OK后,使用自己的域名访问下yii2.0中advanced中的requir ...
- img图片之间的间距问题
[问题]页面中如果有多张图片,那么图片之间会有一些间距,在某些情况下(如切好的图片再次拼接),在显示上就会出现一些问题.效果如下: 对应代码: <div class="f0" ...
- thinkphp接手机网站接口
首先说下,支付宝提供的demo不一定完全正确,可能回缺少些步骤,所以,最好按照规则文档的步骤参照demo写. 先说遇到的问题: 1.错误0001,缺少partner.service等必参数 最初选择的 ...