当前位置:网站首页>比较版本号[双指针截取自己想要的字串]
比较版本号[双指针截取自己想要的字串]
2022-07-01 21:42:00 【REN_林森】
前言
确定begin/end,就确定了一个字串,用双指针取截取符合条件的字串。当然双指针作用不仅于此。
一、比较版本号

二、双指针
package everyday.doublePoint;
// 比较版本号
public class CompareVersion {
/* target:比较version1和version2,大于返回1,小于返回-1,相等返回0。 两者比较不同于普通的字符串比较,需要去掉前导0。 */
public int compareVersion(String version1, String version2) {
int i1 = 0, i2 = 0;
int j1 = i1, j2 = i2;
while (j1 < version1.length() || j2 < version2.length()) {
// 去掉前导为0.
while (i1 < version1.length() && version1.charAt(i1) == 0) ++i1;
// 取数
while (++j1 < version1.length() && version1.charAt(j1) != '.') ;
// 后序没有版本号视为0
String s1 = i1 >= version1.length() ? "" : version1.substring(i1, j1);
int val1 = s1 == "" ? 0 : Integer.parseInt(s1);
// 更新i1/j1
i1 = ++j1;// 走过省略号。
// 同理,i2/j2如此。
while (i2 < version2.length() && version2.charAt(i2) == 0) ++i2;
while (++j2 < version2.length() && version2.charAt(j2) != '.') ;
String s2 = i2 >= version2.length() ? "" : version2.substring(i2, j2);
int val2 = s2 == "" ? 0 : Integer.parseInt(s2);
// 更新i2/j2
i2 = ++j2;// 走过省略号。
// 开始比较
if (val1 > val2) return 1;
if (val1 < val2) return -1;
}
return 0;
}
}
总结
1)双指针。
参考文献
[1] LeetCode 比较版本号
边栏推荐
猜你喜欢

从MLPerf谈起:如何引领AI加速器的下一波浪潮

Application of real estate management based on 3D GIS

Why does blocprovider feel similar to provider?

杰理之、产线装配环节【篇】

MySQL series transaction log redo log learning notes

Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue

编程英语生词笔记本

Aidl basic use

mysql 学习笔记-优化之SQL优化

Pytest Collection (2) - mode de fonctionnement pytest
随机推荐
PHP reflective XSS, reflective XSS test and repair
I received a letter from CTO inviting me to interview machine learning engineer
EMC-电路保护器件-防浪涌及冲击电流用
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
以飞地园区为样本,看雨花与韶山如何奏响长株潭一体化发展高歌
News classification based on LSTM model
One of the basic learning of function
K-means based user portrait clustering model
指标陷阱:IT领导者易犯的七个KPI错误
List announced | outstanding intellectual property service team in China in 2021
pytest合集(2)— pytest运行方式
基于K-means的用户画像聚类模型
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
PCB线路板塞孔工艺的那些事儿~
UVM教程
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
【单体】流辰信息I-BPSv3服务器推荐配置
杰理之烧录都使用 VBAT 供电,供电电压 4.2V【篇】
mysql 学习笔记-优化之SQL优化
基础—io密集型计算和cpu密集型计算