【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 ...
随机推荐
- Linux内核--网络栈实现分析(六)--应用层获取数据包(上)
本文分析基于内核Linux 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7541907 更多请看专栏,地址http: ...
- 'sessionFactory' or 'hibernateTemplate' is required解决方法
这种情况就是在通过spring配置hibernate4的时候(注意,这里是hibernate4不是hibernate3,hibernate3的),使用的是HibernateDaoSupport的这种方 ...
- IIS负载均衡ARR路由请求到ARR服务器和处理服务器
.net web 使用IIS ARR(Application Request Route)技术实现web的高性能.高可靠.易扩展及负载均衡.ARR的使用请参考 IIS负载均衡-Application ...
- 谁能完全搞懂Visual Studio的安装项?
大家都知道,Visual Studio绝对不是“一个程序”那么简单,不管哪个版本,安装好之后总会在“删除程序”中生成一大堆你或懂或不懂的东西.但很少人关注过究竟包括哪些东西.我最近装了一次Visual ...
- rsync permission denied created directories have no permissions
这个问题坑了好几天 最后被此贴解决 http://stackoverflow.com/questions/5798807/rsync-permission-denied-created-directo ...
- 透析Express.js
前言 最近,本屌在试用Node.js,在寻找靠谱web框架时发现了Express.js.Express.js在Node.js社区中是比较出名web框架,而它的定位是“minimal and flexi ...
- 将网站添加到iPhone的主屏幕上
我之前有篇文章介绍过如何将网站固定到Windows的开始菜单,并可以自定义图标.颜色以及Windows推送通知,其实Apple也有类似的功能,通过在网页的head部分添加link标记,在Safari浏 ...
- JS中的闭包
在复习JS高程的过程中,在第七章闭包那节,发现自己看不太明白了.之前看高程的时候,都弄得挺清楚了,怎么现在反而又看不懂了. 嗯,也许更深层次的东西涉及到编译原理的知识了.嗯,在研究完SPA后就开始学习 ...
- EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~终结~配置的优化和事务里读写的统一
回到目录 本讲是通过DbCommand拦截器来实现读写分离的最后一讲,对之前几篇文章做了一个优化,无论是程序可读性还是实用性上都有一个提升,在配置信息这块,去除了字符串方式的拼接,取而代之的是sect ...
- Atitit usbQb212 oo 面向对象封装的标准化与规范解决方案java c# php js
Atitit usbQb212 oo 面向对象封装的标准化与规范解决方案java c# php js 1.1. 封装性是面象对象编程中的三大特性之一 三个基本的特性:封装.继承与多态1 1.2. 魔 ...