当前位置:网站首页>0双指针 LeetCode844. 比较含退格的字符串
0双指针 LeetCode844. 比较含退格的字符串
2022-07-23 05:45:00 【18阿鲁】
分析
从后先前遍历,用time记录遇到#的次数,若Time > 0 或者当前是#则跳过,直到不满足上述条件,然后取比较两个字符串的字符是否是相同的
class Solution {
public boolean backspaceCompare(String s, String t) {
int i = s.length() - 1, j = t.length() - 1;
int times1 = 0, times2 = 0;
while (i >= 0 || j >= 0) {
while (i >= 0 && (s.charAt(i) == '#' || times1 > 0)) {
if (s.charAt(i) == '#') {
times1++;
} else {
times1--;
}
i--;
}
while (j >= 0 && (t.charAt(j) == '#' || times2 > 0)) {
if (t.charAt(j) == '#') {
times2++;
} else {
times2--;
}
j--;
}
if (i >= 0 && j >= 0 && (s.charAt(i) != t.charAt(j))) {
return false;
}
i--;
j--;
}
if (i == j) {
return true;
}
return false;
}
}
```
边栏推荐
猜你喜欢

unity3d:向量计算,AOE图形相交

【基于UDS服务的BootLoader架构和刷写流程】

【AUTOSAR COM 1.通信协议栈介绍】

Interpretation of the paper: recognition of enhancer promoter interactions with neural networks based on pre trained DNA vectors and attention mechanisms

Unity3D高清渲染管线无法在模型上播放视频

Anonymous upper computer V7 waveform display

Using one-way linked list to realize queue

【AUTOSAR之FEE(非易失存储器Flash与Eeprom区别)】

输入三角形边长,求面积

Basic OJ exercise of binary tree-
随机推荐
大小写字母转换
Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 2
[AUTOSAR com 3. signal sending and receiving process tx/rx]
Common sorting method - Select Sorting
Uni native plug-in development -- Youmeng one click login
Using one-way linked list to realize queue
Review of basic principles of steel structure
Implementation of heap and heap sorting
使用InfluxDB数据库的疑惑
Blog Building II: next theme related settings beta
【AUTOSAR COM 1.通信协议栈介绍】
OSI开放系统互联模型和TCP/IP模型
C语言数据库:详细的说明用学生管理系统了解数据库的操作,简单易懂。
Redis——基础概念
合成中文识别数据集的相关repo
主机字节序的判定
刷题笔记:二叉树的中序遍历(三种解法-递归,迭代,Morris)
C language small project - student achievement management system
Blog building six: the method of binding your own domain name
[introduction to AUTOSAR com 4.com service layer module]