找到最近的有相同的X或Y坐标的点Java语言

题目

问题:给你两个整数 x 和 y ,表示你在一个笛卡尔坐标系下的 (x, y) 处。同时,在同一个坐标系下给你一个数组 points ,其中 points[i] = [ai, bi] 表示在 (ai, bi) 处有一个点。当一个点与你所在的位置有相同的 x 坐标或者相同的 y 坐标时,我们称这个点是 有效的 。

要求:请返回距离你当前位置 曼哈顿距离 最近的 有效 点的下标(下标从 0 开始)。如果有多个最近的有效点,请返回下标 最小 的一个。如果没有有效点,请返回 -1 。

要点:两个点 (x1, y1) 和 (x2, y2) 之间的 曼哈顿距离 为 abs(x1 - x2) + abs(y1 - y2) 。

示例

示例 1:

输入:x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
输出:2
解释:所有点中,[3,1],[2,4] 和 [4,4] 是有效点。有效点中,[2,4] 和 [4,4] 距离你当前位置的曼哈顿距离最小,都为 1 。[2,4] 的下标最小,所以返回 2 。

示例 2:

输入:x = 3, y = 4, points = [[3,4]]
输出:0
提示:答案可以与你当前所在位置坐标相同。

示例 3:

输入:x = 3, y = 4, points = [[2,3]]
输出:-1
解释:没有 有效点。

提示:

1 <= points.length <= 104
points[i].length == 2
1 <= x, y, ai, bi <= 104

解题思路及代码

package com.dream;
/**
 * 
 * 2022年3月2日
 * @company
 * @author dream
 * @description
 * 
 */
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//示例1
		int x,y;
		int[][]points1= {{1,2},{3,1},{2,4},{2,3},{4,4}};
		x=3;
		y=4;
		
		Solution s=new Solution();
		int output1=s.nearestValidPoint(x, y, points1);
		System.out.println(output1);
		//示例2
		int[][]points2= {{3,4}};
		x=3;
		y=4;
		
		int output2=s.nearestValidPoint(x, y, points2);
		System.out.println(output2);
		//示例3
		int[][]points3= {{2,3}};
		x=3;
		y=4;
		
		int output3=s.nearestValidPoint(x, y, points3);
		System.out.println(output3);
		
		//其他思想
		System.out.println("--------------其它思想------------------");
		other();
		
	}
	
	private  static void other() {
		//示例1
		int x,y;
		int[][]points1= {{1,2},{3,1},{2,4},{2,3},{4,4}};
		x=3;
		y=4;
		
		Solution1 s=new Solution1();
		int output1=s.nearestValidPoint(x, y, points1);
		System.out.println(output1);
		//示例2
		int[][]points2= {{3,4}};
		x=3;
		y=4;
		
		int output2=s.nearestValidPoint(x, y, points2);
		System.out.println(output2);
		//示例3
		int[][]points3= {{2,3}};
		x=3;
		y=4;
		
		int output3=s.nearestValidPoint(x, y, points3);
		System.out.println(output3);
	}

}
//dream
//思想:暴力解决,代码简单易懂
class Solution {
    public int nearestValidPoint(int x, int y, int[][] points) {
    	//声明一个距离变量并初始化
        int[]sum=new int[points.length];
        for(int i=0;i<points.length;i++) {
        	sum[i]=-1;
        }
        //int index=0;//sum的索引
        //计算曼哈顿距离的平方值,也可以用绝对值Math.abs()求绝对值的方式
        for(int i=0;i<points.length;i++) {
        	if(x==points[i][0]) {
        		sum[i]=(points[i][1]-y)*(points[i][1]-y);
        	}else {
        		if(y==points[i][1]) {
        			sum[i]=(points[i][0]-x)*(points[i][0]-x);
        		}
        	}
        }
        
        double min=Math.pow(10,8);//记录最小曼哈顿距离的平方值
        int minIndex=-1;//记录最小值的索引
        for(int i=0;i<points.length;i++) {
        	if(sum[i]!=-1) {
        		if(sum[i]<min) {
        			min=sum[i];
        			minIndex=i;
        		}
        	}
        }
        if(minIndex==-1) {
        	return -1;
        }else {
        	return minIndex;
        }
    	
    }
}

//另一种思路:
//优点:用一个方法计算曼哈顿距离,foreach循环,代码更简洁
class Solution1{
	//曼哈顿距离计算
	private int getManhattanDistance(int x,int y,int[]point) {
		return Math.abs(point[0]-x)+Math.abs(point[1]-y);
	}
	public int nearestValidPoint(int x,int y,int[][] points) {
		
		int minManhattanDist=Integer.MAX_VALUE;//声明一个最小曼哈顿变量
		int index=-1;//最小值索引
		int i=0;
		for(int[]p:points) {
			if(p[0]==x||p[1]==y) {
				int curDist=getManhattanDistance(x, y, p);
				if(minManhattanDist>curDist) {
					minManhattanDist=curDist;
					index=i;
				}
			}
			i++;
		}
		return index;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值