当前位置:网站首页>比较版本号[双指针截取自己想要的字串]
比较版本号[双指针截取自己想要的字串]
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 比较版本号
边栏推荐
- 业务可视化-让你的流程图'Run'起来
- 以飞地园区为样本,看雨花与韶山如何奏响长株潭一体化发展高歌
- Simple interactive operation of electron learning (III)
- “丝路正青春 风采看福建”在闽外籍青年短视频大赛火热征集作品中
- Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan
- pytest合集(2)— pytest运行方式
- AirServer手机第三方投屏电脑软件
- 打出三位数的所有水仙花数「建议收藏」
- Medium pen test questions: flip the string, such as ABCD, print out DCBA
- 4. 对象映射 - Mapping.Mapstercover
猜你喜欢

首席信息官对高绩效IT团队定义的探讨和分析

JS how to get a list of elements in a collection object

4. 对象映射 - Mapping.Mapstercover

Significance and measures of security encryption of industrial control equipment

PWN攻防世界cgpwn2

最近公共祖先(LCA)在线做法

按照功能对Boost库进行分类

MySQL learning notes - SQL optimization of optimization

Go — 相关依赖对应的exe

Flume面试题
随机推荐
Accelera Systems Initiative是一个独立的非营利组织
locust 系列入门
MySQL empties table data
The difference between NiO and traditional IO
从20s优化到500ms,我用了这三招
PCB线路板塞孔工艺的那些事儿~
Burpsuite simple packet capturing tutorial [easy to understand]
Aidl basic use
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]
十三届蓝桥杯B组国赛
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
Tops, the unit of computing power of the processor, can be carried out 1 trillion times per second
require与import的区别和使用
信标委云原生专题组组长,任重道远!
【STM32】STM32CubeMX教程二–基本使用(新建工程点亮LED灯)
linux下清理系统缓存并释放内存
Go - exe corresponding to related dependency
工控设备安全加密的意义和措施
都能看懂的LIS(最长上升子序列)问题[通俗易懂]
“丝路正青春 风采看福建”在闽外籍青年短视频大赛火热征集作品中