mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
@ -159,7 +159,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
que.push(leftNode->left); // 添加p节点的左子树节点
|
que.push(leftNode->left); // 添加p节点的左子树节点
|
||||||
que.push(rightNode->left); // 添加q节点的左子树节点点
|
que.push(rightNode->left); // 添加q节点的左子树节点
|
||||||
que.push(leftNode->right); // 添加p节点的右子树节点
|
que.push(leftNode->right); // 添加p节点的右子树节点
|
||||||
que.push(rightNode->right); // 添加q节点的右子树节点
|
que.push(rightNode->right); // 添加q节点的右子树节点
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
# 925.长按键入
|
# 925.长按键入
|
||||||
题目链接:https://leetcode-cn.com/problems/long-pressed-name/
|
题目链接:https://leetcode-cn.com/problems/long-pressed-name/
|
||||||
|
|
||||||
你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。
|
你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。
|
||||||
|
|
||||||
你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。
|
你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。
|
||||||
@ -98,7 +99,36 @@ public:
|
|||||||
# 其他语言版本
|
# 其他语言版本
|
||||||
|
|
||||||
Java:
|
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:
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
|
@ -169,6 +169,7 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Python
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def commonChars(self, words: List[str]) -> List[str]:
|
def commonChars(self, words: List[str]) -> List[str]:
|
||||||
|
Reference in New Issue
Block a user