赞
踩
题目:传送门
思路:记录’X‘值之外字符的坐标(或者双指针)
代码:
c++(记录坐标)
class Solution { public: vector<node> v, v1; bool canTransform(string s, string e) { for(int i = 0; i < s.length(); i++) { if(s[i] == 'X')continue; v.push_back(node(i, s[i])); } for(int i = 0; i < e.length(); i++) { if(e[i] == 'X')continue; v1.push_back(node(i, e[i])); } if(v1.size() != v.size())return false; for(int i = 0; i <v1.size(); i++) { if(v1[i].c != v[i].c)return false; else if(v1[i].c == 'R' && v1[i].p < v[i].p) { return false; } else if(v1[i].c == 'L' && v1[i].p > v[i].p) { return false; } } return true; } };
python(双指针)
class Solution: def canTransform(self, s: str, e: str) -> bool: i = 0 j = 0 n = len(s) while i < n and j < n: while i < n and s[i] == 'X': i += 1 while j < n and e[j] == 'X': j += 1 if i >= n or j >= n: continue if s[i] == e[j]: if s[i] == 'L' and i < j: return False if s[i] == 'R' and i > j: return False else: return False i += 1 j += 1 while i < n and s[i] == 'X': i += 1 while j < n and e[j] == 'X': j += 1 if i < n or j < n: return False return True
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。