当前位置:网站首页>Double pointer analog
Double pointer analog
2022-06-24 08:51:00 【accumulate steadily ض】
844. Compare strings with backspace
Use the double finger method to traverse the two strings respectively
- Definition skip To record #, To offset
- Define two pointers , Start traversing from back to front
- There are three situations when two pointers are traversed
1. The first time I met # ---> Record #
2.skip The number of is not 0 --> Offset
3. First encounter with non #--> Jump out of the loop and start comparing
- Finally, compare the characters between the two strings
class Solution {
public boolean backspaceCompare(String s, String t) {
int skipS =0;// Record s In the string # No. quantity
int skipT =0;// Record t In the string # No. quantity
// Two pointers traverse the string from back to front
int i =s.length()-1;// Point to s--> Traversal string s
int j =t.length()-1;// Point to t--> Traversal string t
while(i>=0||j>=0){// Both strings are traversed before ending , Jump out of the loop to prove that the string is equal
// The string s To deal with
//1. The first time I met # ---> Record #
//2.skip The number of is not 0 --> Offset
//3. First encounter with non #--> Jump out of the loop and start comparing
while(i>=0){
if(s.charAt(i)=='#') {
skipS++;
i--;
}else if(skipS>0) {
skipS--;
i--;
}else {
break;
}
}
// Same for j Do the same thing
while(j>=0){
if(t.charAt(j)=='#'){
skipT++;
j--;
}else if(skipT>0){
skipT--;
j--;
}else {
break;
}
}
// At this point, the strings are processed accordingly, and then the two strings are compared
if(i>=0&&j>=0){
if(s.charAt(i)!=t.charAt(j)){
// Two characters are not equal
return false;
}
}else {
if((i>=0&&j<0)||(j>=0&&i<0)){
// Prove that a string has exceeded the limit
return false;
}
}
// Continue traversing the string
i--;
j--;
}
return true;
}
}边栏推荐
- Background management of uniapp hot update
- À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL
- 216. 组合总和 III-枚举法
- 常用表情符号
- 【量化投资】离散傅里叶变换求数组周期
- 什么是SRE?一文详解SRE运维体系
- JS to find and update the specified value in the object through the key
- GradScaler MaxClipGradScaler
- Xiaohei ai4code code baseline nibble 1
- PHP代码加密的几种方案
猜你喜欢

Pymysql inserts data into MySQL and reports an error for no reason

every()、map()、forEarch()方法。数组里面有对象的情况

Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)

数据库迁移从PostgreSQL迁移到 MYSQL

【NOI模拟赛】寄(树形DP)

110. 平衡二叉树-递归法

Distributed | how to make "secret calls" with dble

opencv最大值滤波(不局限于图像)

liunx服务器 telnet 带用户名 端口登陆方法

The form image uploaded in chorme cannot view the binary image information of the request body
随机推荐
微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
Using skills of xargs -- the way to build a dream
The form image uploaded in chorme cannot view the binary image information of the request body
数据中台:数据中台全栈技术架构解析,附带行业解决方案
Get screen width and height tool class
It is enough to read this article about ETL. Three minutes will let you understand what ETL is
提高INSERT速度
偶然间得到的framework工具类 自用
À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL
RuntimeError: Missing dependencies:XXX
MySQL | 存储《康师傅MySQL从入门到高级》笔记
Rsync for file backup
Solving linear equations with MATLAB ax=b
mysql写的代码数据 增删查改等等
【生活思考】计划与自律
Scheduled database backup script
Distributed | how to make "secret calls" with dble
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
2022 spring recruitment interview summary
Change SSH port number