查看: 1130| 回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] LeetCode Biweekly Contest 130 "3143. Maximum Points Inside the Square" Runtime

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x

请问为什么我写的代码,会发生Runtime Error呢?
在`points = [[1,1],[-1,-1],[2,-2]]`和`s="abdca"`的情况下,我在本地测试是没有问题的,但是在LeetCode上提交的时候,会发生以下的Runtime Error。
```
Line 1037: Char 34: runtime error: addition of unsigned offset to 0x50c000000100 overflowed to 0x50c0000000d8 (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_vector.h:1046:34
```

```cpp

代码:
```cpp
class Solution {
struct Point {
  char tag;
  vector<int> coordinates;
  int dist;
};

public:
    int maxPointsInsideSquare(vector<vector<int>>& points, string s) {
        vector<Point> points_with_tag(s.size());
        for (int i = 0; i < (int)s.size(); i++) {
          points_with_tag[i].tag = s[i];
          points_with_tag[i].coordinates = points[i];
          points_with_tag[i].dist = distance(points[i]);
        }

        // sort the points by distance from (0, 0) in the accending order
        sort(points_with_tag.begin(), points_with_tag.end(), [](Point& p1, Point& p2){
          return p1.dist < p2.dist;
        });

        // for (int  i = 0; i < (int)s.size(); i++) {
        //  cout << points_with_tag[i].tag << " " << points_with_tag[i].coordinates[0] << " " << points_with_tag[i].coordinates[1] << endl;
        // }


        unordered_map<char, bool> already_appeared;
        // Initialize already_appeared
        for (int i = 0; i < (int)s.size(); i++) {
          already_appeared[points_with_tag[i].tag] = false;
        }

        int count = 0;

        // search from nearest to farthest: if the points have the same tag with already-visited points, then that is the maximum square
        int i = 0;
        for (i = 0; i < (int)s.size(); i++) {
          if (already_appeared[points_with_tag[i].tag]) {
            break;
          } else {
            already_appeared[points_with_tag[i].tag] = true;
            count++;
            continue;
          }
        }

        // the case there are two points with the same distance from (0, 0)
        //////
        // a 2 2
        // b -1 -2
        // c -3 1
        // a 3 -3
        // d -4 4
        //////
        // the above case, for loop breaks  in (3,-3) and it returns 3, but actually (-3, 1) has the same distance from (0, 0) with (3, -3) so this points should not be included in the maximum square.
        while(points_with_tag[i].dist == points_with_tag[i-1].dist) {
          i--;
          count--;
        }

        return count;
    }
    // without static, the following fun cannot be used in `sort`.
    // non-static member function does belong to some specific object but static member function does not belong to any object and can be called without any object.
    static int distance(vector<int>& point) {
      return (abs(point[0])) > (abs(point[1])) ? abs(point[0]) : abs(point[1]);
    }
};
```

多谢帮助!




上一篇:新手问下一个关于实习刷题的问题
下一篇:分享一个个人感觉比较直白的题目分类树
🔗
 楼主| xyz_xyj 2024-5-12 07:56:08 | 只看该作者
全局:
【因为帖子里面的代码部分没有显示正确,而不能修改已发送的帖子,重新再这里发一次】


请问为什么我写的代码,会发生Runtime Error呢?
  1. points = [[1,1],[-1,-1],[2,-2]]
复制代码
  1. s="abdca"
复制代码
的情况下,我在本地测试是没有问题的,但是在LeetCode上提交的时候,会发生以下的Runtime Error。
  1. Line 1037: Char 34: runtime error: addition of unsigned offset to 0x50c000000100 overflowed to 0x50c0000000d8 (stl_vector.h)
  2. SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_vector.h:1046:34
复制代码
代码:
  1. class Solution {
  2. struct Point {
  3.   char tag;
  4.   vector<int> coordinates;
  5.   int dist;
  6. };
  7. public:
  8.     int maxPointsInsideSquare(vector<vector<int>>& points, string s) {
  9.         vector<Point> points_with_tag(s.size());
  10.         for (int i = 0; i < (int)s.size(); i++) {
  11.           points_with_tag[i].tag = s[i];
  12.           points_with_tag[i].coordinates = points[i];
  13.           points_with_tag[i].dist = distance(points[i]);
  14.         }
  15.         // sort the points by distance from (0, 0) in the accending order
  16.         sort(points_with_tag.begin(), points_with_tag.end(), [](Point& p1, Point& p2){
  17.           return p1.dist < p2.dist;
  18.         });
  19.         // for (int  i = 0; i < (int)s.size(); i++) {
  20.         //  cout << points_with_tag[i].tag << " " << points_with_tag[i].coordinates[0] << " " << points_with_tag[i].coordinates[1] << endl;
  21.         // }
  22.         unordered_map<char, bool> already_appeared;
  23.         // Initialize already_appeared
  24.         for (int i = 0; i < (int)s.size(); i++) {
  25.           already_appeared[points_with_tag[i].tag] = false;
  26.         }
  27.         int count = 0;
  28.         // search from nearest to farthest: if the points have the same tag with already-visited points, then that is the maximum square
  29.         int i = 0;
  30.         for (i = 0; i < (int)s.size(); i++) {
  31.           if (already_appeared[points_with_tag[i].tag]) {
  32.             break;
  33.           } else {
  34.             already_appeared[points_with_tag[i].tag] = true;
  35.             count++;
  36.             continue;
  37.           }
  38.         }
  39.         // the case there are two points with the same distance from (0, 0)
  40.         //////
  41.         // a 2 2
  42.         // b -1 -2
  43.         // c -3 1
  44.         // a 3 -3
  45.         // d -4 4
  46.         //////
  47.         // the above case, for loop breaks  in (3,-3) and it returns 3, but actually (-3, 1) has the same distance from (0, 0) with (3, -3) so this points should not be included in the maximum square.
  48.         while(points_with_tag[i].dist == points_with_tag[i-1].dist) {
  49.           i--;
  50.           count--;
  51.         }
  52.         return count;
  53.     }
  54.     // without static, the following fun cannot be used in `sort`.
  55.     // non-static member function does belong to some specific object but static member function does not belong to any object and can be called without any object.
  56.     static int distance(vector<int>& point) {
  57.       return (abs(point[0])) > (abs(point[1])) ? abs(point[0]) : abs(point[1]);
  58.     }
  59. };
复制代码
多谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表