leetcode _Max Points on a Line

题目

Given  n  points on a 2D plane, find the maximum number of points that lie on the same straight line.

思路

这道题交了N遍才过。

maxPoints1:是O(n^3)的暴力法,枚举不同的两点,确定一条直线,然后对所有点进行点积判断是否共线(点积为0),共线则累加。超时()

maxPoints2:枚举一个点,再枚举另一个点,只需记录这两点斜率是否出现过,用map保存

注意问题

这题比较容易出错的是
1、两个相同点,是算两次,因此在枚举的时候遇到相同的点时,要记录改点的重复次数
2、对于斜率不能表示的直线(两点的x值相同),需要单独记录这些点

3、应该用unordered_map,它的效率比map更高

代码

class Solution{
public:
   double EPS;
    int  maxPoints(vector<Point> &points)
    {
        EPS = 1e-6;
        return  maxPoints2(points);
    }

    bool is_in_a_line(Point &p1,Point &p2,Point &p0)
    {
        if (fabs((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y))<EPS)
                return true;
            else
                return false;
    }

    int maxPoints1(vector<Point> &points)
    {
        //enumerate the line,then for every point tell if it is on this line
        //two possibility:
        //1.p1 equls p2
        //2.p1 not equls p3
        int max_np=0;
        for(vector<Point>::iterator p1=points.begin();p1!=points.end();++p1)
            for(vector<Point>::iterator p2=p1+1;p2!=points.end();++p2)
        {
            if (!(p1->x==p2->x && p1->y==p2->y))
                {
                    int np =0;
                    for(vector<Point>::iterator p3=points.begin();p3!=points.end();++p3)
                    {
                            if (is_in_a_line(*p1,*p2,*p3) ) np++;
                    }
                    if (np>max_np)  max_np = np;
                }
                else{
                     int np =0;
                    for(vector<Point>::iterator p3=points.begin();p3!=points.end();++p3)
                    {
                            if (p1->x==p2->x && p1->y==p2->y) np++;
                    }
                    if (np>max_np)  max_np = np;
                }
        }
        return max_np;
    }

    int maxPoints2(vector<Point> &points)
    {
            map<double,int> slope_map;
            int max_np=0;
            for(vector<Point>::iterator p1=points.begin();p1!=points.end();++p1)
            {
                slope_map.clear();
                int points_of_vertical_line = 0;
                int same_points = 0;
                int cur_max_num=0;
                for(vector<Point>::iterator p2=points.begin();p2!=points.end();++p2)
                {
                    if (!(p1->x==p2->x && p1->y==p2->y))
                      {
                          //discuss the vetical line
                          if (p1->x==p2->x)
                            points_of_vertical_line ++;
                            else {
                                double slope = (p2->y - p1->y) * 1.0 / ( p2->x - p1->x );
                                  /*if (slope_map.find(slope) == slope_map.end())
                                    slope_map.insert(pair<double,int>(slope,1));
                                  else*/
                                    slope_map[slope]++;
                                 if (slope_map[slope] >cur_max_num) cur_max_num = slope_map[slope];
                            }
                      }
                      else
                        same_points ++;
               /*     cur_max_num  =   points_of_vertical_line ;
                    for(map<double,int>::iterator it=slope_map.begin();it!=slope_map.end();++it)
                        if(it->second > cur_max_num) cur_max_num = it->second;*/
                            if (points_of_vertical_line>cur_max_num) cur_max_num = points_of_vertical_line;
                }
                if (same_points + cur_max_num>max_np) max_np =same_points + cur_max_num;
            }
            return max_np;
    }
};

疑问

 做这题最后也是参考的标准答案再写的,但是心里很疑惑 存储浮点数的时候,容器会不会出精度问题呢?unordered_map我觉得应该不会,实现机制是Hash,但是map呢?欢迎讨论



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述: 给定一个字符串,请将字符串里的字符按照出现的频率降序排列。 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和't'都只出现一次。因此'e'必须出现在'r'和't'之前。此外,"eetr"也是一个有效的答案。 示例 2: 输入: "cccaaa" 输出: "cccaaa" 解释: 'c'和'a'都出现三次。此外,"aaaccc"也是有效的答案。注意"cacaca"是不正确的,因为相同的字母必须放在一起。 示例 3: 输入: "Aabb" 输出: "bbAa" 解释: 此外,"bbaA"也是一个有效的答案,但"Aabb"是不正确的。注意'A'和'a'被认为是两种不同的字符。 Java代码如下: ``` import java.util.*; public class Solution { public String frequencySort(String s) { if (s == null || s.length() == 0) { return ""; } Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); map.put(c, map.getOrDefault(c, 0) + 1); } List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue()); StringBuilder sb = new StringBuilder(); for (Map.Entry<Character, Integer> entry : list) { char c = entry.getKey(); int count = entry.getValue(); for (int i = 0; i < count; i++) { sb.append(c); } } return sb.toString(); } } ``` 解题思路: 首先遍历字符串,使用HashMap记录每个字符出现的次数。然后将HashMap转换为List,并按照出现次数从大到小进行排序。最后遍历排序后的List,将每个字符按照出现次数依次添加到StringBuilder中,并返回StringBuilder的字符串形式。 时间复杂度:O(nlogn),其中n为字符串s的长度。遍历字符串的时间复杂度为O(n),HashMap和List的操作时间复杂度均为O(n),排序时间复杂度为O(nlogn),StringBuilder操作时间复杂度为O(n)。因此总时间复杂度为O(nlogn)。 空间复杂度:O(n),其中n为字符串s的长度。HashMap和List的空间复杂度均为O(n),StringBuilder的空间复杂度也为O(n)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值