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)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【LeetCode】Reconstruct Itinerary(332)

    1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...

  5. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  6. 【LeetCode】Counting Bits(338)

    1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...

  7. 【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 ...

  8. 【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 ...

  9. 【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 ...

随机推荐

  1. UIAlertController 使用

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...

  2. Oracle基本数据字典:v$database、v$instance、v$version、dba_objects

    v$database: 视图结构: SQL> desc v$database; Name                                      Null?    Type - ...

  3. dp跟px的互相转换

    一 获取手机屏幕的密度 1 获取屏幕的宽和高,然后根据 直角三角形的 a边的平方+b边的平方=c边的平方 得到另一条边的长:然后除以 ,屏幕的尺寸,就是 手机的密度destity 2 根据上下文获取c ...

  4. 团队项目——站立会议DAY6

    团队项目--站立会议 DAY6        团队成员介绍(5人):张靖颜.何玥.钟灵毓秀.赵莹.王梓萱        今日(2016/5/13),站立会议已进行了一周时间,大家将这一周所遇到的问题和 ...

  5. java提高篇(二五)-----HashTable

          在java中与有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key ...

  6. Behind RabbitMQ Exchange Types

    what's the underlying philosophy behind "exchange types"? In a word, it is all about imple ...

  7. Hibernate SQL实际sql语句监控- p6spy+hibernate+proxool 设置

    由于ORM工具的缘故,我们调试程序的时候远没有直接在程序里直接写个string的SQL简单,想当年查个sql是有多么的幸福,一行sql = "select * from ..."找 ...

  8. Oracle常见名词解析

    创建用户 概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用. create user 用户名 identified by 密码; ...

  9. 第1讲 Redis部署与基本操作

    目录 一.简介 二.安装 1.默认安装位置 2.指定安装位置 3.安装的可执行文件的作用 三.启动与关闭 四.配置文件 五.Redis的数据类型 1. 共计5种类型 2. String(子串类型) 3 ...

  10. Atitit 类库冲突解决方案  httpclient-4.5.2.jar

    Atitit 类库冲突解决方案  httpclient-4.5.2.jar 错误提示如下1 版本如下(client and selenium)2 解决流程2 挂载源码 (SSLConnectionSo ...