【问题标题】:Reorder Data in Log Files(Leetcode)重新排序日志文件中的数据(Leetcode)
【发布时间】:2021-12-25 07:02:48
【问题描述】:
class Solution
{
public:
    bool cmp(string str1, string str2)
    {
        int sp1, sp2;
        for (int i = 0; i < str1.length(); i++)
        {
            if (str1[i] == ' ')
            {
                sp1 = i;
                break;
            }
        }
        for (int i = 0; i < str2.length(); i++)
        {
            if (str2[i] == ' ')
            {
                sp2 = i;
                break;
            }
        }
        string s1 = "", s2 = "";
        for (int i = sp1 + 1; i < str1.length(); i++)
            s1 += str1[i];
        for (int i = sp2 + 1; i < str2.length(); i++)
            s2 += str2[i];
        return s1 <= s2;
    }
    vector<string> reorderLogFiles(vector<string> &logs)
    {
        int n = logs.size();
        vector<string> ansLog(n);
        int start = 0, end = n - 1;

        for (auto str : logs)
        {
            reverse(str.begin(), str.end());
            if (str[0] >= 48 && str[0] <= 57)
            {
                reverse(str.begin(), str.end());
                ansLog[end] = str;
                end--;
            }
            else
            {
                reverse(str.begin(), str.end());
                ansLog[start] = str;
                start++;
            }
        }
        
        end++;
        reverse(ansLog.begin() + end, ansLog.end());
        sort(ansLog.begin(), ansLog.begin() + start, cmp);
        return ansLog;
    }
};

谁能帮助我为什么我在 sort(ansLog.begin(), ansLog.begin() + start, cmp); 。我正在使用比较器对向量从开始到索引 start-1 进行排序。
问题链接:https://leetcode.com/problems/reorder-data-in-log-files/

【问题讨论】:

    标签: c++ sorting data-structures


    【解决方案1】:

    不专注于解决问题,而只专注于错误。如果您也发布错误会更有帮助。 但希望你的错误如下:

    error: reference to non-static member function must be called
            sort(ansLog.begin(), ansLog.begin() + start, cmp);
                                                         ^~~
    1 error generated.
    

    这里的问题是 cmp 方法在这里是非静态类成员,您需要将其与 this 和它需要的两个参数绑定。可以有多种解决方案:

    1. 您可以考虑使用 boost::function 来绑定 this:
    sort(ansLog.begin(), ansLog.begin() + start,  boost::bind( &Solution::cmp, this, _1, _2 ));
    
    1. 由于此函数不使用任何类属性,您可以简单地将其设为静态并按照您的使用方式使用它。
    2. 我认为更好的方法是使用如下仿函数:
        class Comparator {
            public:
            bool operator()(string str1, string str2) {
                int sp1, sp2;
                for (int i = 0; i < str1.length(); i++)
                {
                    if (str1[i] == ' ')
                    {
                        sp1 = i;
                        break;
                    }
                }
                for (int i = 0; i < str2.length(); i++)
                {
                    if (str2[i] == ' ')
                    {
                        sp2 = i;
                        break;
                    }
                }
                string s1 = "", s2 = "";
                for (int i = sp1 + 1; i < str1.length(); i++)
                    s1 += str1[i];
                for (int i = sp2 + 1; i < str2.length(); i++)
                    s2 += str2[i];
                return s1 <= s2;
            }
        };
    

    并将其用作:

    sort(ansLog.begin(), ansLog.begin() + start, Comparator());
    

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 1970-01-01
      • 2012-01-17
      • 2020-04-02
      • 2015-12-24
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多