Comments (2)
这题我想了一上午,三种方法来搞都没跑通。
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.
是的,文字解释里的这个逻辑写反了。刚改正了过来。谢谢提醒。
from leetcode.
Related Issues (20)
- Leetcode 87 solution can not pass currently HOT 1
- 链接失效问题 HOT 1
- 递归更简洁 HOT 2
- Leetcode 156 直接递归,代码更简洁直观。 HOT 1
- 1846 可以用sorting + one pass解决 HOT 1
- 115.Distinct-Subsequences HOT 1
- 1798. belongs to Greedy, not Two Pointers? HOT 1
- 260- Single Numbers III HOT 2
- 940过不了 HOT 1
- 关于heap分类的疑惑 HOT 2
- 关于二分查找代码的疑问 HOT 7
- Question about the for loop condition to efficientlly iterate all subset HOT 2
- Leetcode 2584.Split-the-Array-to-Make-Coprime-Products HOT 2
- 699 解法1 更新 right+1 边界时,未考虑是否会破坏原 right +1 HOT 1
- Important Leetcode Practice
- 双指针解法疑问 HOT 3
- 532 KDiffPairInAnArray HOT 2
- 1569.Number-of-Ways-to-Reorder-Array-to-Get-Same-BST.cpp | Long Code -> Concise Code
- Add redis store library for basic operations HOT 2
- DD HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode.