【LeetCode每日一题】[简单]925. 长按键入
题目来源 算法思想:字符串
题目: 思路: 两个指针,分别指向两个字符串,依次比较指针上的字母
看了答案,发现自己写语句的又长又臭,简洁版入下:
class Solution { public boolean isLongPressedName(String name, String typed) { char[] tmp1 = name.toCharArray(); char[] tmp2 = typed.toCharArray(); int i = 0;//指向name int j = 0;//指向typed int len1 = tmp1.length;//name长度 int len2 = tmp2.length;//typed长度 while (j < len2) {//j在范围内遍历 if (i < len1 && tmp2[j] == tmp1[i]) {//i在合理分为内,如果ij指向字母相同,同时向后移动 i++; j++; } else if (j > 0 && tmp2[j] == tmp2[j-1]) {//如果,字母不相同,但是是重复字母,j++; j++; } else {//其他情形均返回false; return false; } } return i == len1;//比较i是否配对到了最后 } }