herrDeng網內搜尋

自訂搜尋

Ads

2024年12月5日 星期四

Python C C++ 2 pointer速解Leetcode 2337 Move Pieces to Obtain a String


Python C C++ 2 pointer速解Leetcode 2337  Move Pieces to Obtain a String
多日Leetcode的問題都可用2-pointer法來解,今日2337. Move Pieces to Obtain a String也不例外,這次就用C/C++/Python三個語言來解[Python解請進]
-------
Many Leetcode problems can be solved using the 2-pointer method. Today’s 2337. Move Pieces to Obtain a String is no exception. This time, we will use C/C++/Python to solve the problem.
  1. class Solution:
  2. def canChange(self, s: str, t: str) -> bool:
  3. n=len(s)
  4. s+='@'
  5. t+='@'
  6. i, j=0, 0
  7. while i<n or j<n:
  8. while i<n and s[i]=='_': i+=1
  9. while j<n and t[j]=='_': j+=1
  10. c=s[i]
  11. if c!=t[j]: return False
  12. if c=='L' and i<j: return False
  13. if c=='R' and i>j: return False
  14. i+=1
  15. j+=1
  16. return True

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章