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.
[codes on Leetcode]https://leetcode.com/problems/move-pieces-to-obtain-a-string/solutions/6114530/2-pointers-nested-loop-beats-100/
[2 pointer/Sliding window playlist]https://www.youtube.com/watch?v=pi1XpQU_Yxo&list=PLYRlUBnWnd5KrCfs7qCFb-Bvn7dvnakFy
class Solution: def canChange(self, s: str, t: str) -> bool: n=len(s) s+='@' t+='@' i, j=0, 0 while i<n or j<n: while i<n and s[i]=='_': i+=1 while j<n and t[j]=='_': j+=1 c=s[i] if c!=t[j]: return False if c=='L' and i<j: return False if c=='R' and i>j: return False i+=1 j+=1 return True
沒有留言:
張貼留言