这是小川的第387次更新,第416篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第248题(顺位题号是1037)。回旋镖是一组各不相同且不在一条直线上的三个点。给出三个点的列表,判断这些点是否是回旋镖。

例如:

输入:[[1,1],[2,3],[3,2]]

输出:true

输入:[[1,1],[2,2],[3,3]]

输出:false

注意

  • points.length == 3

  • points[i].length == 2

  • 0 <= points[i][j] <= 100

02 第一种解法

要判断三个点是否能构成回旋镖,一是三个点各不相同,二是不能在一条直线上,即二维数组中三对坐标两两值不相等,且任意两对坐标的斜率不能相等。直接将题目的意思翻译过来即可,但是在计算两点的斜率时,如果分母为0或者分子为0,即这两个点可能与x轴、y轴平行,需要返回false。另外算斜率要用double类型,不能用int类型。

public boolean isBoomerang(int[][] points) {
int x1 = points[0][0], x2 = points[1][0], x3 = points[2][0];
int y1 = points[0][1], y2 = points[1][1], y3 = points[2][1];
// 坐标两两不等
if ((x1 == x2 && y1 == y2) || (x2==x3 && y2==y3) || (x1==x3 && y1==y3)) {
return false;
}
// 不能和x轴、y轴平行
if ((x1 == x2 && x2 == x3) || (y1 == y2 && y2 == y3)) {
return false;
}
// 计算斜率
double slope = (y2-y1)/((double)x2-x1);
double slope2 = (y3-y2)/((double)x3-x2);
return slope != slope2;
}

03 第二种解法

还可以从另外的角度来看,依旧是数学角度,根据题目给的回旋镖定义,要是三个点能够组成一个回旋镖,那么三个点合成的区域一定是一个三角形。那么我们可以计算出三条边的长度,用三角形的定义来判断三条边能否组成一个三角形。

public boolean isBoomerang2(int[][] points) {
int x1 = points[0][0], x2 = points[1][0], x3 = points[2][0];
int y1 = points[0][1], y2 = points[1][1], y3 = points[2][1];
// 计算三边的长度
double a = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
double b = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
double c = Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
boolean flag = a+b>c && a+c>b && b+c>a;
boolean flag2 = a-b<c && a-c<b && b-c<a;
return flag && flag2;
}

04 小结

算法专题目前已连续日更超过七个月,算法题文章254+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.1037-有效的回旋镖(Valid Boomerang)的更多相关文章

  1. 【Leetcode_easy】1037. Valid Boomerang

    problem 1037. Valid Boomerang 参考 1. Leetcode_easy_1037. Valid Boomerang; 完

  2. LeetCode 1037. Valid Boomerang (有效的回旋镖)

    题目标签:Math 题目给了我们三个点,让我们判断这三个点是否在一条直线上. 利用斜率 k = (y1 - y0) / (x1 - x0) 来判断,如果 三个点 abc, ab 的斜率 = bc 的斜 ...

  3. 【LeetCode】1037. Valid Boomerang 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中学数学题 日期 题目地址:https://leet ...

  4. 【leetcode】1037. Valid Boomerang

    题目如下: A boomerang is a set of 3 points that are all distinct and not in a straight line. Given a lis ...

  5. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  6. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

    题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  7. LeetCode 20. 有效的括号(Valid Parentheses)

    20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...

  8. leetcode第31题--Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

随机推荐

  1. 泛型 class TimeComparator<Asr> implements Comparator<Asr>

    class TimeComparator<Asr> implements Comparator<Asr> 为何需要改为 class TimeComparator impleme ...

  2. POJ3311Hie with the Pie(floyd传递+DP,状态压缩)

    问题 The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfo ...

  3. python时间 time模块和datetime模块

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  4. JAVA笔记7-Object类之toString方法和equals方法

    位于rt.jar\java\lang\Object.class Object类是所有Java类的根基类(Java中只有一个根基类,C++中有多个).如果在类声明中未使用extends关键字指明其基类, ...

  5. 【C#-读取XML文件】XMLReader读取XML文档

    使用   XmlReader.Create("文件路径")   加载xml文件 XmlReader使用流的方式来读取. //使用XMLReader读取XML数据 XmlReader ...

  6. 最新天猫3轮面试题目:虚拟机+并发锁+Sql防注入+Zookeeper

    天猫一面 自我介绍.项目介绍 Spring拦截器.实现了哪些方法?底层原理 AOP如何配置,底层原理.2种动态代理,aop注解实现,xml定义切面 Bean的作用域,单例模式是否线程安全?恶汉模式是否 ...

  7. 科普TPF知识

    https://tieba.baidu.com/p/4926092734?see_lz=1&pn=1 707680700 https://tieba.baidu.com/p/492609273 ...

  8. __stdcall、__cdcel、__fastcall 调用

    常用的调用约定有stdcall,cdecl,fastcall,thiscall,naked call等,以下将 __stdcall.__cdecl和__fastcall三种函数调用协议加以比较,函数调 ...

  9. C++:std::map的遍历

    for (auto &kv : myMap) { count<<kv.first<<" has value "<<kv.second&l ...

  10. Open Cascade 转化为OpenSceneGraph中的Mesh

    #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osgGA/StateSetManipul ...