当前位置:网站首页>比较版本号[双指针截取自己想要的字串]
比较版本号[双指针截取自己想要的字串]
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 比较版本号
边栏推荐
- CNN卷积神经网络原理讲解+图片识别应用(附源码)[通俗易懂]
- K-means based user portrait clustering model
- 编程英语生词笔记本
- 人才近悦远来,望城区夯实“强省会”智力底座
- Burpsuite simple packet capturing tutorial [easy to understand]
- Test cancellation 1
- [STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
- 股票手机开户哪个app好,安全性较高的
- 为什么数字化转型战略必须包括持续测试?
- String类型转换BigDecimal、Date类型
猜你喜欢
随机推荐
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
Wechat applet, continuously playing multiple videos. Synthesize the appearance of a video and customize the video progress bar
String类型转换BigDecimal、Date类型
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
Accelera Systems Initiative是一个独立的非营利组织
vscode的使用
MySQL empties table data
C中main函数的几种写法
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
杰理之烧录上层版物料需要【篇】
burpsuite简单抓包教程[通俗易懂]
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
选择在同花顺上炒股开户可以吗?安全吗?
Separate the letters and numbers in the string so that the letters come first and the array comes last
AirServer2022最新版功能介绍及下载
Simple interactive operation of electron learning (III)
[noip2013] building block competition [noip2018] road laying greed / difference
杰理之烧录都使用 VBAT 供电,供电电压 4.2V【篇】
MQ学习笔记
MQ learning notes