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. 用MOS管防止电源反接的原理

    电源反接,会给电路造成损坏,不过,电源反接是不可避免的.所以,我么就需要给电路中加入保护电路,达到即使接反电源,也不会损坏的目的. 一般可以使用在电源的正极串入一个二极管解决,不过,由于二极管有压降, ...

  2. Git凭证存储(简单易懂,一学就会,认真看)

    今天给自己提了一个问题,当我们在github.com或者gitlab上面新建仓库,并克隆到本地,首次使用的时候,会被问及用户名密码,但是这两个信息存在哪里呢? 带着这个问题,我开始搜索,并在<P ...

  3. rhel 5.8下静默安装oracle11gr2

    1.图形界面下录制脚本如下: #-------------------------------------------------------------------------------# Do ...

  4. 通过Measure & Arrange实现UWP瀑布流布局

    简介 在以XAML为主的控件布局体系中,有用于完成布局的核心步骤,分别是measure和arrange.继承体系中由UIElement类提供Measure和Arrange方法,并由其子类Framewo ...

  5. 图解集合5:不正确地使用HashMap引发死循环及元素丢失

    问题引出 前一篇文章讲解了HashMap的实现原理,讲到了HashMap不是线程安全的.那么HashMap在多线程环境下又会有什么问题呢? 几个月前,公司项目的一个模块在线上运行的时候出现了死循环,死 ...

  6. 第二次作业:Github的优点和缺点

    ---恢复内容开始--- GitHub的优势和劣势 简介: Github是一个代码托管平台和开发者社区,开发者可以在Github上创建自己的开源项目并与其他开发者协作编码.创业公司可以用它来托管软件项 ...

  7. 解决Win7 软件图标不显示--Win7图标异常,快捷方式不显示解决方法

    电脑症状:WIN7的系统,桌面上的图标显示的不正常,快捷方式显示的是未知程序.看不到程序默认图标,快捷方式图标不显示. 解决方法:删除程序图标缓存即可.   将下面的内容复制到记事本保存为“Repai ...

  8. [翻译].NET随机数

    原文链接:http://csharpindepth.com/Articles/Chapter12/Random.aspx   随机数 当你在Stack Overflow上看到看到某个问题标题当中有“随 ...

  9. 12小时包你学会基于ReactMix框架的ReactNativeApp开发(一)Hello World!

    ReactMixhttps://github.com/xueduany/react-mix自从昨天发布起来,得到了不少小伙伴的热捧,很高兴帮助大家解决了憋在心中很久的问题“如果我只会HTML,Css, ...

  10. 微信授权步骤与详解 -- c#篇

    微信授权步骤与详解 -- c#篇 注:这里不涉及界面操作,只介绍代码操作. 1.基本原理如下: 从图上所知,第一步用户访问我们的网页,第二步我们后台跳转到微信授权页面,第三步用户点击授权,第四步微信重 ...