Giter Site home page Giter Site logo

Comments (2)

mingyEx avatar mingyEx commented on May 22, 2024

这题我想了一上午,三种方法来搞都没跑通。

1.两边dp

要两边dp,记录每个L/R 经过滑动可以出现的位置,然后检查target[i],如果是L就检查l[],否则检查r[],看对应位置是否可能相同。
但是对'_'的处理不正确,因为_是可能随着L的移动而变化的。

class Solution {
public:
  bool canChange(string s, string t) {
    string l(1, s.back()), r(1, s[0]);
    int sl = count(all(s), 'L');
    int sr = count(all(s), 'R');
    int tl = count(all(t), 'L');
    int tr = count(all(t), 'R');
    if (sl != tl || sr != tr)
      return false;

    //处理R
    for (int i = 1; i < s.size(); ++i)
      if (r.back() == 'R' && s[i] == '_')
        r += 'R';
      else
        r += s[i];
    //处理L
    for (int i = s.size() - 2; i >= 0; --i)
      if (l.back() == 'L' && s[i] == '_')
        l += 'L';
      else
        l += s[i];
    std::reverse(all(l));
    for (int i = 0; i < t.size(); ++i)
    {
      if (t[i] == 'L' && t[i] == l[i])continue;
      if (t[i] == 'R' && t[i] == r[i])continue;
      if (t[i] == '_'&&s[i]=='_') continue;
      return false;
    }
    return true;
  }
};

2.双指针

分别扫描L/R,要求它在s里出现的位置必须小于/大于 t里出现的位置

class Solution {
public:
  bool canChange(string s, string t) {
    int sl = count(all(s), 'L'),sr = count(all(s), 'R'),tl = count(all(t), 'L'),tr = count(all(t), 'R');
    if (sl != tl || sr != tr)return false;

    //r从左向右扫,t里存在的都挪动s.
    int r = 0;
    for (int i = 0; i < t.size(); ++i)
    {
      if (t[i] == 'R') {
        while (r < s.size() && s[r] != 'R')++r;
        if (r == t.size())return false;
        ++r;
      }
    }

    //l从右往左扫描,t里存在的都往前挪s
    int l = t.size() - 1;
    for (int i = t.size() - 1; i >= 0; --i)
    {
      if (t[i] == 'L') {
        while (l >= 0 && s[l] != 'L')--l;
        if (l == 0)return false;
        --l;
      }
    }
    //这个扫法也有问题,对于
    //"R_L_"
    //  "__LR"
    //  该是0 却返回1,双指针初试失败
    return true;
  }
};

跟您的答案可以说就差亿点点了。

from leetcode.

wisdompeak avatar wisdompeak commented on May 22, 2024

是的,文字解释里的这个逻辑写反了。刚改正了过来。谢谢提醒。

from leetcode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.