当前位置:网站首页>比较版本号[双指针截取自己想要的字串]
比较版本号[双指针截取自己想要的字串]
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 比较版本号
边栏推荐
- Fundamentals - IO intensive computing and CPU intensive computing
- Icml2022 | interventional contrastive learning based on meta semantic regularization
- Medium pen test questions: flip the string, such as ABCD, print out DCBA
- 【生态伙伴】鲲鹏系统工程师培训
- vscode的使用
- Go — 相关依赖对应的exe
- [NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
- 基于LSTM模型实现新闻分类
- Talking from mlperf: how to lead the next wave of AI accelerator
- K-means based user portrait clustering model
猜你喜欢
随机推荐
An operation tool used by we media professionals who earn 1w+ a month
MySQL series transaction log redo log learning notes
一次调试去了解redis集群的slot机制
最近公共祖先离线做法(tarjan)
Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan
linux下清理系统缓存并释放内存
物联网rfid等
"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition
Unity 使用Sqlite
Flume面试题
UVM教程
AIDL基本使用
Count the number of each character in the character
月入1W+的自媒体达人都会用到的运营工具
Application of real estate management based on 3D GIS
杰理之烧录都使用 VBAT 供电,供电电压 4.2V【篇】
MySQL系列之事务日志Redo log学习笔记
Manually implement function isinstanceof (child, parent)
PWN攻防世界cgpwn2
JS how to get a list of elements in a collection object