当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢

数据中台:数据中台技术架构详解

110. 平衡二叉树-递归法

【PyTorch基础教程30】DSSM双塔模型代码解析

Prompt code when MySQL inserts Chinese data due to character set problems: 1366
![[pytoch basic tutorial 31] youtubednn model analysis](/img/18/dbeab69894583f6e5230772ce44652.png)
[pytoch basic tutorial 31] youtubednn model analysis

【牛客】把字符串转换成整数

教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5

“不平凡的代理初始值设定不受支持”,出现的原因及解决方法

Jenkins自动化部署,连接不到所依赖的服务【已解决】

WebRTC系列-网络传输之5选择最优connection切换
随机推荐
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
Database migration from PostgreSQL to MySQL
pymysql 向MySQL 插入数据无故报错
【量化投资】离散傅里叶变换求数组周期
Distributed | how to make "secret calls" with dble
Liunx change the port number of vsftpd
[life thinking] planning and self-discipline
Ordering of MySQL composite index
mysql组合索引的有序性
数据中台:数据中台技术架构详解
One development skill a day: how to establish P2P communication based on webrtc?
【NOI模拟赛】给国与时光鸡(构造)
There was an error checking the latest version of pip
解析互联网广告术语 CPM、CPC、CPA、CPS、CPL、CPR 是什么意思
[pytorch basic tutorial 30] code analysis of DSSM twin tower model
微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
MySQL | 视图《康师傅MySQL从入门到高级》笔记
Analyze the meaning of Internet advertising terms CPM, CPC, CPA, CPS, CPL and CPR
【NOI模拟赛】寄(树形DP)
Win11 blank when using VIM to view content in cmder