1

请教大佬一个 leetcode 上 c++解答的问题

 1 year ago
source link: https://www.v2ex.com/t/865597
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  问与答

请教大佬一个 leetcode 上 c++解答的问题

  yaron · 44 分钟前 · 113 次点击
image-20220712103628586

输入:"L__R__R", "L______RR" 输出:false 但在本地调试,输出是 true ,是代码哪里出错了吗?

bool canChange(string start, string target) {
        //长度一定要相同
        if (start.size() != target.size())
            return false;

        int num = start.size();
        int i, j = 0;
        while (i < num && j < num)
        {
            while (i < num && start[i] == '_')
                i++;
            while (j < num && target[j] == '_')
                j++;

            // L 和 R 的排序一定要相同
            if (start[i] != target[j])
                return false;
            // R 只能往右移,所以下标一定要小,才能往右移( i + x = j )
            if (start[i] == 'R' && i > j)
                return false;
            // L 只能往左移,所以下标一定要大,才能往左移( i - x = j )
            if (start[i] == 'L' && i < j)
                return false;

            i++;
            j++;
        }

        while (i < num)
        {
            if (start[i++] != '_')
                return false;
        }
        while (j < num)
        {
            if (target[j++] != '_')
                return false;
        }
        return true;
    }

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK