作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/valid-boomerang/

题目描述

A boomerang is a set of 3 points that are all distinct and not in a straight line.

Given a list of three points in the plane, return whether these points are a boomerang.

Example 1:

Input: [[1,1],[2,3],[3,2]]
Output: true

Example 2:

Input: [[1,1],[2,2],[3,3]]
Output: false

Note:

  1. points.length == 3
  2. points[i].length == 2
  3. 0 <= points[i][j] <= 100

题目大意

判断三个点是否不重叠,并且不在一个直线上。

解题方法

中学数学题

不重叠很好说,三个点两两判断即可。

判断三个点是不是在一个直线上,那么三个点中任意两个点的连线之斜率是否相等(或者不存在)。用数学公式表示就是dx1 * dy2 == dx2 * dy1则在一条直线上。

C++代码如下:

class Solution {
public:
bool isBoomerang(vector<vector<int>>& points) {
if (points[0][0] == points[1][0] && points[0][1] == points[1][1])
return false;
if (points[0][0] == points[2][0] && points[0][1] == points[2][1])
return false;
if (points[1][0] == points[2][0] && points[1][1] == points[2][1])
return false;
int dx1 = points[1][0] - points[0][0];
int dy1 = points[1][1] - points[0][1];
int dx2 = points[2][0] - points[1][0];
int dy2 = points[2][1] - points[1][1];
return dx1 * dy2 != dx2 * dy1;
}
};

日期

2019 年 8 月 31 日 —— 赶在月底做个题

【LeetCode】1037. Valid Boomerang 解题报告(C++)的更多相关文章

  1. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

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

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

  3. LeetCode 242 Valid Anagram 解题报告

    题目要求 Given two strings s and t , write a function to determine if t is an anagram of s. 题目分析及思路 给出两个 ...

  4. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  5. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【Leetcode_easy】1037. Valid Boomerang

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

  9. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

随机推荐

  1. Pacbio三代基因组组装简介

    参考: 视频PPT来自欧易生物讲座:如何开启一个动植物基因组三代de novo项目?

  2. python3安装,支持openssl,支持采集https

    python3安装,支持openssl,支持采集https 坑好多,特别是安装的时候,各种不匹配,服务器默认配置是python2,升级3后,采集的时候用到openssl,花了两天也没搞定各种错误,也许 ...

  3. 最短剩余时间优先法则SRTN

  4. 关于JSONObject的性能问题

    现有一段代码: private JSONObject override(User user, UserVO vo) { String json = JSON.toJSONString(vo); JSO ...

  5. SpringBoot整合Shiro 一:搭建环境

    Java项目的安全框架一般使用 shiro 与 spring security 具体怎么选择可以参考文章:安全框架 Shiro 和 Spring Security 如何选择 我这里选择使用Shiro ...

  6. 单体内置对象 Global 和 Math

    单体内置对象 Global 和 Math 在所有代码执行前,作用域中就已经存在两个内置对象:Global(全局)和Math.在大多数ES实现中都不能直接访问Global对象.不过,WEB浏览器实现了承 ...

  7. PS只能各个工具使用的注意知识点

    1.图章工具  <仿制图章工具>使用方法:按住alt点击吸取干净的地方,然后松开alt键,按住鼠标左键拖动或左击  擦拭 图章区域放大缩小,是按住alt键+鼠标右键左右滑动 当图片中多个图 ...

  8. Appium获取toast消息(二)

    刚接触appium进行移动端设备的UI自动化,在遇到toast消息的时候很是苦恼了一阵,最后通过强大的搜索引擎找到了个相对解决方法,废话不多说,直接贴代码↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ...

  9. 可落地的DDD代码实践

    目录 前言 一.从六边形架构谈起 二.依赖倒置 三.DDD 代码分层 3.1 用户接口层 3.2 应用层 3.2 1 Response vs Exception 3.2.2 CQE vs DTO 3. ...

  10. JavaMoney规范(JSR 354)与对应实现解读

    一.概述 1.1 当前现状 当前JDK中用来表达货币的类为java.util.Currency,这个类仅仅能够表示按照**[ISO-4217]**描述的货币类型.它没有与之关联的数值,也不能描述规范外 ...