925. 长按键入

it2025-04-12  21

LeetCode: 925. 长按键入

题目 easy 题, 但通过率很低

说难其实不难 >> 主要是理清双指针各自遍历的思路

双指针

双指针

slow >> 遍历 name fast >> 遍历 typed

public boolean isLongPressedName(String name, String typed) { int slow = 0, fast = 0; int nlen = name.length(), tlen = typed.length(); while (slow < nlen && fast < tlen){ // 如果两个不相等 if(name.charAt(slow) != typed.charAt(fast)) return false; // 记录需要跳过几个相同的 int skipn = 0, skipt = 0; while (slow + 1 < nlen && name.charAt(slow) == name.charAt(slow + 1)){ skipn++; slow++; } while (fast + 1< tlen && typed.charAt(fast) == typed.charAt(fast + 1)){ skipt++; fast++; } if(skipn > skipt) return false; slow++; fast++; } if(fast < tlen || slow < nlen) return false; return true; }


>> 解题思路

最新回复(0)