简体   繁体   English

返回 str 与 str.substr(0,str.size()) 在 leetcode 中给了我不同的输出

[英]returning str vs. str.substr(0,str.size()) gives me different outputs in leetcode

I just solved https://leetcode.com/problems/push-dominoes/ .我刚刚解决了https://leetcode.com/problems/push-dominoes/

My code is我的代码是

class Solution {
public:

    string pushDominoes(string dominoes) {
        string res(dominoes.size(), ' ');
        dominoes = 'L' + dominoes + 'R';

        int l = 0;
        for(int r = 1; r < dominoes.size(); r++)
        {
            if(dominoes[r] != '.')
            {
                //check which case we have [L...R], [R....L] [L.....L]  [R...R]
                if(l != 0) res[l-1] = dominoes[l]; 
                if(r != dominoes.size() - 1) res[r-1] = dominoes[r];

                if(dominoes[l] == dominoes[r])
                {
                    for(int i = l; i <= r; i++) 
                        if(i > 0) res[i-1] = dominoes[r];
                }
                else if(dominoes[l] == 'L' && dominoes[r] == 'R')
                    for(int i = l+1; i < r; i++) res[i-1] = '.';
                else if(dominoes[l] == 'R' && dominoes[r] == 'L')
                {
                    if((l+r)%2 == 0) 
                    {
                        auto mid = (l+r)/2;
                        res[mid-1] = '.';
                        for(int i = l+1; i < mid; i++) res[i-1] = 'R';
                        for(int i = mid+1; i < r; i++) res[i-1] = 'L';
                    }
                    else
                    {
                        auto mid = (l+r)/2;
                        for(int i = l+1; i <= mid; i++) res[i-1] = 'R';
                        for(int i = mid+1; i < r; i++) res[i-1] = 'L';
                    }
                }

                l = r;
            }
        }

        return res;
        // return res.substr(0, res.size());
    }
};

int main( ) 
{ 
   Solution soln;

   cout << soln.pushDominoes(".L.R.") << endl;
}

For one of the test cases, where the input is ".LR" leetcode states that my output is "LL.RRRLLRRLL.." when I use return res .对于其中输入为“.LR”的测试用例之一,leetcode 指出当我使用return res时,我的输出为“LL.RRRRLLRRLL..”。 The answer is supposed to be "LL.RR".答案应该是“LL.RR”。 But I printed out res and it's indeed "LL.RR" and size 5.但是我打印出res并且它确实是“LL.RR”和大小 5。

If I change my code to return return res.substr(0, res.size()) instead of return res , I get the correct solution.如果我将代码更改为 return return res.substr(0, res.size())而不是return res ,我会得到正确的解决方案。 I'm perplexed as to why this is occuring when these 2 return statements should be identical?当这两个返回语句应该相同时,我很困惑为什么会发生这种情况?

I also ported the code over and compiled it on my computer, and I get the correct solution.我还移植了代码并在我的计算机上编译它,我得到了正确的解决方案。 This makes me wonder if there's some differences in how my code is handled between different C++ compilers, or if Leetcode might have some issues for this particular problem.这让我想知道我的代码在不同 C++ 编译器之间的处理方式是否存在一些差异,或者 Leetcode 是否可能对这个特定问题有一些问题。 Any advice?有什么建议吗?

string res(dominoes.size(), ' ');

Sets the size of res to that of dominoes and this size is never changed.res的大小设置为dominoes的大小,并且这个大小永远不会改变。 res can never be larger than dominoes . res永远不会大于dominoes With input of ".LR", dominoes 's length is 5 .输入“.LR”, dominoes的长度为 5 。 res is length 5. res是长度 5。

dominoes = 'L' + dominoes + 'R';

Changes the size of dominoes to 7.dominoes的大小更改为 7。

for (int r = 1; r < dominoes.size(); r++)

iterates r from 1 to 6.r从 1 迭代到 6。

Aside: When ever I see a <= in a for loop I stop for a longer look.旁白:当我在for循环中看到<= ,我会停下来仔细看看。 It's wrong far more often than it is right.错误的次数远远多于正确的次数。

In this case it seems to be only sort of wrong, allowing在这种情况下,它似乎只是一种错误,允许

for (int i = l; i <= r; i++)
    if (i > 0) res[i - 1] = dominoes[r];

to iterate i from l to r .il迭代到r Since r can be 6, i can be 6. That means因为r可以是 6,所以i可以是 6。这意味着

    if (6 > 0) res[6 - 1] = dominoes[6];

is possible.是可能的。 This resolves to这解决了

    res[5] = dominoes[6];

and res[5] is not valid.并且res[5]无效。 Writing there invokes undefined behaviour, and in this case it appears to overwrite the null terminator of the c-style string backing the string .在那里写入会调用未定义的行为,在这种情况下,它似乎覆盖了支持string的 c 样式字符串的空终止string

Another aside: Avoid using l as a variable name.另一个旁白:避免使用l作为变量名。 It looks too much like 1 and leads to bugs or misreading of the code.它看起来太像1并导致错误或代码误读。

To fix, you could expand the logic of if (i > 0) into if (i > 0 && i-1 < res.size()) to filter out this problem, but you're better off reworking or replacing the algorithm so that you never find yourself in this situation.要解决此问题,您可以将if (i > 0)的逻辑扩展为if (i > 0 && i-1 < res.size())以过滤掉此问题,但最好重新设计或替换算法,以便你永远不会发现自己处于这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM