博客
关于我
2种解法 - 获取一条直线上最多的点数
阅读量:389 次
发布时间:2019-03-05

本文共 2740 字,大约阅读时间需要 9 分钟。

重新优化后的内容

解决方案

给定平面上的n个点,找出最多有多少个点在同一条直线上。这是一个经典的几何问题,可以通过计算点的斜率来解决,但由于浮点数精度问题,通常会采用分数形式来处理斜率。

方法一:斜率法

  • 思路:以某个点为基础点,计算其他点与之连线的斜率。如果两个点的斜率相同,则说明它们在同一直线上。使用浮点数计算斜率,可能会因为精度问题导致错误,特别是当斜率非常接近时。

  • 步骤

    • 以第一个点为基础点,计算所有其他点与之连线的斜率。
    • 如果有多个点的斜率相同,则这些点在同一直线上。
    • 统计最大点数,并考虑基础点重复出现的情况。
  • 优点:简单易懂,时间复杂度为O(n²)。

    • 缺点:可能因浮点数精度问题导致错误,特别是斜率接近的情况。
  • 方法二:分数形式斜率

  • 思路:将斜率转换为最简分数形式,避免浮点数精度问题。通过分子和分母的最简形式来唯一表示每条直线。

  • 步骤

    • 使用辗转相除法计算分子和分母的最大公约数。
    • 将分子和分母同时除以最大公约数,得到最简分数形式。
    • 将分子和分母转换为字符串,作为唯一标识每条直线的键。
    • 处理垂直线的情况,直接构建字符串。
  • 优点:避免了浮点数精度问题,时间复杂度为O(n²)。

    • 缺点:计算最大公约数的时间增加,代码复杂度提高。
  • 代码实现

    using System;using System.Collections.Generic;using System.Linq;public class Solution{    public int MaxPoints(int[][] points)    {        if (points.Length == 1) return 1;        Dictionary
    lineCount = new Dictionary
    (); List
    pointList = new List
    (); Dictionary
    pointRepeat = new Dictionary
    (); for (int i = 0; i < points.Length; i++) { string pointKey = $"{points[i][0]}_{points[i][1]}"; if (pointList.Contains(pointKey)) { if (pointRepeat.ContainsKey(pointKey)) pointRepeat[pointKey]++; else pointRepeat[pointKey] = 1; } else { pointList.Add(pointKey); } } int maxPoints = 1; Dictionary
    currentLine = new Dictionary
    (); int maxTemp = 1; for (int i = 0; i < points.Length; i++) { currentLine.Clear(); int tmp = 1; for (int j = i + 1; j < points.Length; j++) { if (points[i][0] == points[j][0] && points[i][1] == points[j][1]) { continue; } int x1 = points[i][0]; int y1 = points[i][1]; int x2 = points[j][0]; int y2 = points[j][1]; if (x1 == x2) { string key = $"v_{y1}_{y2}"; if (currentLine.ContainsKey(key)) { currentLine[key]++; } else { currentLine[key] = 1; } if (currentLine[key] + 1 > maxTemp) { maxTemp = currentLine[key] + 1; } } else { int up = y2 - y1; int down = x2 - x1; int gcd = GCD(down, up); int simplifiedUp = up / gcd; int simplifiedDown = down / gcd; string key = $"{simplifiedUp}_{simplifiedDown}"; if (currentLine.ContainsKey(key)) { currentLine[key]++; } else { currentLine[key] = 1; } if (currentLine[key] + 1 > maxTemp) { maxTemp = currentLine[key] + 1; } } } foreach (var kvp in currentLine) { if (kvp.Value > maxTemp) { maxTemp = kvp.Value; } } string pointKey = $"{points[i][0]}_{points[i][1]}"; if (pointRepeat.ContainsKey(pointKey)) { maxTemp += pointRepeat[pointKey]; } else { pointRepeat[pointKey] = 1; } if (maxTemp > maxPoints) { maxPoints = maxTemp; } } return maxPoints; } private static int GCD(int a, int b) { while (a != 0) { int temp = a; a = b % a; b = temp; } return b; }}

    代码解释

    • 类成员:使用多个字典来存储点的重复次数、直线的点数以及最简分数斜率。
    • 重复点处理:记录每个点的坐标字符串,统计重复点的次数。
    • 斜率计算:计算每条直线的斜率,并用最简分数形式存储,避免浮点数精度问题。
    • 最大值统计:统计每条直线上的点数,并更新最大点数。

    这种方法通过将斜率转换为最简分数形式,避免了浮点数精度问题,确保了计算的准确性。同时,代码结构清晰,易于维护和扩展。

    转载地址:http://ufzzz.baihongyu.com/

    你可能感兴趣的文章
    Netty的网络框架差点让我一夜秃头,哭了
    查看>>
    Netty相关
    查看>>
    Netty简介
    查看>>
    Netty线程模型理解
    查看>>
    netty解决tcp粘包和拆包问题
    查看>>
    Netty速成:基础+入门+中级+高级+源码架构+行业应用
    查看>>
    Netty遇到TCP发送缓冲区满了 写半包操作该如何处理
    查看>>
    netty(1):NIO 基础之三大组件和ByteBuffer
    查看>>
    Netty:ChannelPipeline和ChannelHandler为什么会鬼混在一起?
    查看>>
    Netty:原理架构解析
    查看>>
    Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
    查看>>
    Network Sniffer and Connection Analyzer
    查看>>
    Network 灰鸽宝典【目录】
    查看>>
    Network-Emulator Network-Emulator-Toolkit网络模拟器使用
    查看>>
    Networkx写入Shape文件
    查看>>
    NetworkX系列教程(11)-graph和其他数据格式转换
    查看>>
    Networkx读取军械调查-ITN综合传输网络?/读取GML文件
    查看>>
    NetworkX:是否为每个节点添加超链接?
    查看>>
    network小学习
    查看>>
    Netwox网络工具使用详解
    查看>>