1037-有效的回旋镖(Valid Boomerang)

该博客讨论了如何解决LeetCode上的'有效的回旋镖'问题,主要涉及Java编程。内容包括题目的描述,示例输入输出,解题策略,以及排除重复和判断三点是否共线的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
中文

回旋镖定义为一组三个点,这些点各不相同且不在一条直线上。

给出平面上三个点组成的列表,判断这些点是否可以构成回旋镖。

英文

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.

示例1

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

示例2

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

提示

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

解题思路

  • 首先判重。
if((points[0][0] == points[1][0] && points[0][1] == points[1][1])
        		|| (points[0][0] == points[2][0] && points[0][1] == points[2][1])
        		|| (points[1][0] == points[2][0] && points[1][1] == points[2][1])){
        	return false;
        }
  • 然后判断是否在一条直线
else {
			int x = points[2][0],y = points[2][1];
			int x1 = points[0][0],y1 = points[0][1];
			int x2 = points[1][0], y2 = points[1][1];
			if((y-y1)*(x2 - x1) == (y2 - y1)*(x - x1))
				return false;
		}
  • 然后就没了

完整代码

class Solution {
    public boolean isBoomerang(int[][] points) {
        if((points[0][0] == points[1][0] && points[0][1] == points[1][1])
        		|| (points[0][0] == points[2][0] && points[0][1] == points[2][1])
        		|| (points[1][0] == points[2][0] && points[1][1] == points[2][1])){
        	return false;
        }
        else {
			int x = points[2][0],y = points[2][1];
			int x1 = points[0][0],y1 = points[0][1];
			int x2 = points[1][0], y2 = points[1][1];
			if((y-y1)*(x2 - x1) == (y2 - y1)*(x - x1))
				return false;
		}
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值