LeetCode_Compare Version Numbers
题目:
Compare two version numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
You may assume that the version strings are non-empty and contain only digits and the
.
character.
The .
character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5
is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
题目属于简单级别,还是挺复杂的,思路:将字符串按照"."分割,将“.”之间的数字转化为整数,然后分别对应比较。例如:1.2和13.37,分别转化为数组[1,2]和[13,37],从前往后对应比较1比13小,这时就可以得出结论1.2<13.37,若相等,则继续向后比较。这里需要注意,若两个字符串转化的数组不等长,需要将短的数组后面补0,例如,1.1和1.1.0,前者转化为[1,1],后者是[1,1,0],这时需要将前者补齐为[1,1,0],然后再对应比较。代码如下:
public int compareVersion(String version1, String version2) {
//.的个数
int dot_num1 = 0;
int dot_num2 = 0;
for(int i = 0;i<version1.length();i++)
{
if(version1.charAt(i)=='.')
dot_num1 ++;
}
for(int i = 0;i<version2.length();i++)
{
if(version2.charAt(i)=='.')
dot_num2 ++;
}
//按照.的位置截取子串
String arr1[] = new String[dot_num1+1];
String arr2[] = new String[dot_num2+1];
//记录每个字符串中的.的个数
int index1[] = new int[dot_num1+2];
int index2[] = new int[dot_num2+2]; int in1 = 1;
index1[0] = -1;
for(int i = 0;i<version1.length();++i)
{
if(version1.charAt(i)=='.')
index1[in1++] = i;
}
index1[in1] = version1.length(); int in2 = 1;
index2[0] = -1;
for(int i = 0;i<version2.length();++i)
{
if(version2.charAt(i)=='.')
index2[in2++] = i;
}
index2[in2] = version2.length(); //截取子串
int index = 0;
for(int i = 0;i<index1.length-1;i++)
{
arr1[index++] = version1.substring(index1[i]+1,index1[i+1]);
} index = 0;
for(int i = 0;i<index2.length-1;i++)
{
arr2[index++] = version2.substring(index2[i]+1,index2[i+1]);
}
int len1 = arr1.length;
int len2 = arr2.length;
int max = len1>len2?len1:len2;
//所有子串转化为整数形式
int num1[] = new int[max];
int num2[] = new int[max];
for(int i = 0;i<len1;i++)
num1[i] = Integer.parseInt(arr1[i]);
for(int i = 0;i<len2;i++)
num2[i] = Integer.parseInt(arr2[i]);
//长度不一,短的后面0补齐
/*
* *
* 例如1.0和1.0.0
* 前者生成的num1数组为[1,0]
* 后者生成的num2数组为[1,0,0]
* 这里将num1补齐为[1,0,0],便于比较
*/
if(len1>=len2)
{
for(int i = len2;i<len1;i++)
{
num2[i] = 0;
}
}
else
{
for(int i = len1;i<len2;i++)
{
num1[i] = 0;
}
}
/*for(int i = 0;i<num2.length;i++)
{
System.out.println(num2[i]);
} for(int i = 0;i<num1.length;i++)
{
System.out.println(num1[i]);
}*/
for(int i = 0;i<max;i++)
{
if(num1[i]>num2[i])
return 1;
if(num1[i]<num2[i])
return -1;
} return 0;
}
LeetCode_Compare Version Numbers的更多相关文章
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- 【leetcode】Compare Version Numbers
题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...
- Compare Version Numbers
Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 & ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java for LeetCode 165 Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- LeetCode Compare Version Numbers
原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...
- leetcode:Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- 【LeetCode】165 - Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- 1.1.18 zabbix监控NFS
1.1.1 zabbix监控NFS 第一步创建脚本: 添加执行权限 chmod +x cat /server/scripts/nfs_check.sh [root@web02 scripts]# ...
- make: *** [sapi/cli/php] Error 1 解决办法
make: *** [sapi/cli/php] Error 1 一:考虑过make clean,问题依然 二:(采取此方法后出现启动apache报错:/usr/local/apache2/modul ...
- python3制作捧腹网段子页爬虫
0x01 春节闲着没事(是有多闲),就写了个简单的程序,来爬点笑话看,顺带记录下写程序的过程.第一次接触爬虫是看了这么一个帖子,一个逗逼,爬取煎蛋网上妹子的照片,简直不要太方便.于是乎就自己照猫画虎, ...
- 超酷的JavaScript叙事性时间轴(Timeline)类库
在线演示 Timeline 是我见过的最酷的展示事件随时间发展的javascript实现.你可以基于时间使用讲故事的方式来创建时间轴特效,整个时间轴以幻灯的方式来展示,你可以穿插图片,视频或者是网站, ...
- dm8127之核间通信syslink
Last updated: June 23, 2010 Contents [hide] 1 About SysLink 1.1 SysLink Architecture 1.2 SysLink Usa ...
- 【BZOJ】1688: [Usaco2005 Open]Disease Manangement 疾病管理(状压dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1688 很水的状压.. 提交了很多次优化的,但是还是100msT_T #include <cst ...
- JQuery获取和设置select下拉框的值
获取Select : 获取select 选中的 text : $("#sid").find("option:selected").text(); 获取selec ...
- win7下cmake编译opencv2.3.1生成opencv—createsamples.exe和opencv_haartrainingd.exe
第一步:下载安装cmake,之后进行默认安装即可,这步略过. 第二步:配置cmake ,使cmake找到opencv进行编译安装 watermark/2/text/aHR0cDovL2Jsb2cuY3 ...
- 《linux系统及其编程》实验课记录(五)
实验 5:权限的设置和更改 实验环境: 安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统.有另外一个无特权用户 student,密码 student 的 ...
- 如何用几何画板画V型尖波函数图像
虽然几何画板没有编程功能,无法直接进行逻辑判断,但通过恰当地运用sgn.trunc等函数可以间接地画出一些特殊函数图像,比如V型尖波函数图像,下面将详细介绍如何用几何画板画V型尖波函数图像. 具体的操 ...