当前位置:网站首页>比较版本号[双指针截取自己想要的字串]
比较版本号[双指针截取自己想要的字串]
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 比较版本号
边栏推荐
- vscode的使用
- 信标委云原生专题组组长,任重道远!
- Introduction and download of the latest version of airserver2022
- 杰理之蓝牙耳机品控和生产技巧【篇】
- Fundamentals - IO intensive computing and CPU intensive computing
- Application of real estate management based on 3D GIS
- require与import的区别和使用
- PHP reflective XSS, reflective XSS test and repair
- MySQL数据库驱动(JDBC Driver)jar包下载
- 4. 对象映射 - Mapping.Mapstercover
猜你喜欢

Little p weekly Vol.11

微软、哥伦比亚大学|GODEL:目标导向对话的大规模预训练

K-means based user portrait clustering model
![[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分](/img/d1/a56231cd4eb3cc1d91d8a55048ccfe.png)
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分

基于YOLOv5的口罩佩戴检测方法

Business visualization - make your flowchart'run'up

运放-滞回(迟滞)比较器全流程实战计算

ngnix基础知识

九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3

MySQL series transaction log redo log learning notes
随机推荐
K-means based user portrait clustering model
人才近悦远来,望城区夯实“强省会”智力底座
MySQL series transaction log redo log learning notes
News classification based on LSTM model
Manually implement function isinstanceof (child, parent)
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
杰理之蓝牙耳机品控和生产技巧【篇】
测试撤销1
安装mysql时出现:需要这两个包perl(Data::Dumper),perl(JSON)
pytest合集(2)— pytest運行方式
Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc
AIDL基本使用
功利点没啥!
【生态伙伴】鲲鹏系统工程师培训
linux下清理系统缓存并释放内存
js数组拼接的四种方法[通俗易懂]
List announced | outstanding intellectual property service team in China in 2021
locust 系列入门
信标委云原生专题组组长,任重道远!
Significance and measures of security encryption of industrial control equipment