Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6fe43e0

Browse files
author
scuyjzh
committed
add solutions to problem - "925. Long Pressed Name"
1 parent e68ada1 commit 6fe43e0

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
| 259 | [较小的三数之和](https://leetcode-cn.com/problems/3sum-smaller/) 🔒 | [Java](./src/com/scuyjzh/leetcode/medium/No_0259_3Sum_Smaller/Solution.java) | 中等 | 数组 双指针 二分查找 排序 |
231231
| 845 | [数组中的最长山脉](https://leetcode-cn.com/problems/longest-mountain-in-array/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0845_Longest_Mountain_in_Array/Solution.java) | 中等 | 数组 双指针 动态规划 枚举 |
232232
| 881 | [救生艇](https://leetcode-cn.com/problems/boats-to-save-people/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0881_Boats_to_Save_People/Solution.java) | 中等 | 贪心 数组 双指针 排序 |
233+
| 925 | [长按键入](https://leetcode-cn.com/problems/long-pressed-name/) | [Java](./src/com/scuyjzh/leetcode/easy/No_0925_Long_Pressed_Name/Solution.java) | 简单 | 双指针 字符串 |
233234

234235
### Array
235236

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.scuyjzh.leetcode.easy.No_0925_Long_Pressed_Name;
2+
3+
/**
4+
* 925. 长按键入
5+
*
6+
* 你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按
7+
* 键可能会被长按,而字符可能被输入 1 次或多次。
8+
*
9+
* 你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字
10+
* (其中一些字符可能被长按),那么就返回 True。
11+
*/
12+
class Solution {
13+
/**
14+
* 方法:双指针
15+
*/
16+
public boolean isLongPressedName(String name, String typed) {
17+
// 使用两个下标 i,j 追踪 name 和 typed 的位置
18+
int i = 0, j = 0;
19+
while (j < typed.length()) {
20+
if (i < name.length() && name.charAt(i) == typed.charAt(j)) {
21+
// 当 name[i]=typed[j] 时,说明两个字符串存在一对匹配的字符,此时将 i,j 都加 1
22+
++i;
23+
++j;
24+
} else if (j > 0 && typed.charAt(j) == typed.charAt(j - 1)) {
25+
// 否则,如果 typed[j]=typed[j−1],说明存在一次长按键入,此时只将 j 加 1
26+
++j;
27+
} else {
28+
// 如果 typed 中存在一个字符,它两个条件均不满足,则应当直接返回 false
29+
return false;
30+
}
31+
}
32+
// 最后,如果 i=name.length,说明 name 的每个字符都被「匹配」了
33+
return i == name.length();
34+
}
35+
36+
public static void main(String[] args) {
37+
System.out.println(new Solution().isLongPressedName("alex", "aaleex"));
38+
System.out.println(new Solution().isLongPressedName("saeed", "ssaaedd"));
39+
System.out.println(new Solution().isLongPressedName("leelee", "lleeelee"));
40+
}
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /