zoukankan      html  css  js  c++  java
  • LeetCode 925 长键按入

    LeetCode 925 长键按入

    问题描述
    你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。
    你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。

    双指针

    • 两个指针p1、p2分别指向name、type中的当前字符,统计、计算数量、比较

    执行用时:1 ms, 在所有 Java 提交中击败了86.83%的用户
    内存消耗:36.5 MB, 在所有 Java 提交中击败了83.05%的用户

    class Solution {
        public boolean isLongPressedName(String name, String typed) {
            if(name == null&&typed==null) {
                return true;
            }
            else if(name==null || typed==null) {
                return false;
            }
            else if(name.length()>typed.length()) {
                return false;
            }
    
            //双指针,p1指向name中当前字符,p2指向typed中当前字符
            int p1 = 0, p2 = 0;
            while(p1 < name.length()) {
                //记录name中当前字符的重复个数
                int n = 1;
                char ch = name.charAt(p1);
                p1++;
                while(p1<name.length() && name.charAt(p1-1)==name.charAt(p1)) {
                    p1++;
                    n++;
                }
                //找到typed中对应的字符重复个数
                while(p2<typed.length() && typed.charAt(p2)==ch) {
                    p2++;
                    n--;
                }
                if(n>0) {
                    return false;
                }
            }
    
            return p2>=typed.length()? true: false;
        }
    }
    
  • 相关阅读:
    PLSQL Developer报错(一)
    HTML中的select下拉框内容显示不全的解决办法
    行链接和行迁移
    读UNDO引发的db file sequential read
    direct path read
    db file scattered read
    分区裁剪
    打开CSDN论坛出现403
    Flex中获取RadioButtonGroup中的RadioButton的值
    Excel 2010去掉网格线
  • 原文地址:https://www.cnblogs.com/CodeSPA/p/13850404.html
Copyright © 2011-2022 走看看