Merge pull request #597 from ironartisan/master

添加0925.长按键入java代码
This commit is contained in:
程序员Carl
2021-08-14 10:54:47 +08:00
committed by GitHub
3 changed files with 33 additions and 2 deletions

View File

@ -159,7 +159,7 @@ public:
return false;
}
que.push(leftNode->left); // 添加p节点的左子树节点
que.push(rightNode->left); // 添加q节点的左子树节点
que.push(rightNode->left); // 添加q节点的左子树节点
que.push(leftNode->right); // 添加p节点的右子树节点
que.push(rightNode->right); // 添加q节点的右子树节点
}

View File

@ -9,6 +9,7 @@
# 925.长按键入
题目链接https://leetcode-cn.com/problems/long-pressed-name/
你的朋友正在使用键盘输入他的名字 name。偶尔在键入字符 c 按键可能会被长按而字符可能被输入 1 次或多次。
你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字其中一些字符可能被长按那么就返回 True。
@ -98,7 +99,36 @@ public:
# 其他语言版本
Java
```java
class Solution {
public boolean isLongPressedName(String name, String typed) {
int i = 0, j = 0;
int m = name.length(), n = typed.length();
while (i< m && j < n) {
if (name.charAt(i) == typed.charAt(j)) { // 相同则同时向后匹配
i++; j++;
}
else {
if (j == 0) return false; // 如果是第一位就不相同直接返回false
// 判断边界为n-1,若为n会越界,例如name:"kikcxmvzi" typed:"kiikcxxmmvvzzz"
while (j < n-1 && typed.charAt(j) == typed.charAt(j-1)) j++;
if (name.charAt(i) == typed.charAt(j)) { // j跨越重复项之后再次和name[i]匹配
i++; j++; // 相同则同时向后匹配
}
else return false;
}
}
// 说明name没有匹配完
if (i < m) return false;
// 说明type没有匹配完
while (j < n) {
if (typed.charAt(j) == typed.charAt(j-1)) j++;
else return false;
}
return true;
}
}
```
Python
```python
class Solution:

View File

@ -169,6 +169,7 @@ class Solution {
}
}
```
Python
```python
class Solution:
def commonChars(self, words: List[str]) -> List[str]: