Self Crossing
2016-05-08 00:00
295 查看
摘要: leetcode Self Crossing
分析:给一个数字数组,按照固定方向和长度判断能否自交。
分三种情况:
1、4条线自交
2、5条线自交
3、6条线自交
更多的数,也包含在这3种情况之下。for循环来判断是否是这三种情况中的一种,若是,则return true;不是则return false
参考博客:http://www.cnblogs.com/grandyang/p/5216856.html
分析:给一个数字数组,按照固定方向和长度判断能否自交。
分三种情况:
1、4条线自交
2、5条线自交
3、6条线自交
更多的数,也包含在这3种情况之下。for循环来判断是否是这三种情况中的一种,若是,则return true;不是则return false
class Solution { public: bool isSelfCrossing(vector<int>& x) { int len = x.size(); for(int i=3;i<len;i++){ if(x[i-3]>=x[i-1]&&x[i]>=x[i-2]) { return true; } if(i>=4&&x[i-4]+x[i]>=x[i-2]&&x[i-1]==x[i-3]) { return true; } if(i>=5&&x[i-5]+x[i-1]>=x[i-3]&&x[i]+x[i-4]>=x[i-2]&&x[i-3]>=x[i-5]&&x[i-3]>=x[i-1]&&x[i-2]>=x[i-4]&&x[i-2]>=x[i]) { return true; } } return false; } };
参考博客:http://www.cnblogs.com/grandyang/p/5216856.html
相关文章推荐
- Ruby self在不同环境的含义
- PHP中new static() 和 new self() 的区别介绍
- javascript中的self和this用法小结
- python self,cls,decorator的理解
- python 参数列表中的self 显式不等于冗余
- php类中的$this,static,final,const,self这几个关键字使用方法
- PHP中static关键字以及与self关键字的区别
- python中self原理实例分析
- Python中为什么要用self探讨
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- PHP中this,self,parent的区别
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum