2016.5.21——Compare Version Numbers

Compare Version Numbers

本题收获:

1.

2.

  题目:

  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

  思路:

  我的思路:将字符串转化为数字比较大小(字符串如何转化为数字不知道怎么操作)

  leetcode/dicuss思路:将字符串转化为数字比较大小,有两种代码实现方式。

  代码:

  代码1:思路很易懂

 1 int compareVersion(string version1, string version2) {
 2     int i = 0;
 3     int j = 0;
 4     int n1 = version1.size();
 5     int n2 = version2.size();
 6
 7     int num1 = 0;
 8     int num2 = 0;
 9     while(i<n1 || j<n2)        //如果有有一个先结束,就结束循环
10     {
11         while(i<n1 && version1[i]!=‘.‘){        //遇到‘.‘结束循环
12             num1 = num1*10+(version1[i]-‘0‘);
13             i++;
14         }
15
16         while(j<n2 && version2[j]!=‘.‘){
17             num2 = num2*10+(version2[j]-‘0‘);;
18             j++;
19         }
20
21         if(num1>num2) return 1;
22         else if(num1 < num2) return -1;
23
24         num1 = 0;            //遇到‘.‘后结束循环,将num置0,重新开始循环,先比较‘.‘之前,在比较‘.‘之后
25         num2 = 0;
26         i++;
27         j++;
28     }
29
30     return 0;
31 }

  结合代码看思路:先比较‘.‘之前的,如果不相等则返回,如果相同继续以同样的方法判断‘.‘之后的。

  代码2:涉及到的较多的库函数,但是很简洁。

 1 class Solution {
 2 public:
 3     int compareVersion(string version1, string version2) {
 4         for (; version1 != version2; version1 = nextSubstr(version1),version2 = nextSubstr(version2)) {    //nextSubstr()是自己定义的函数,在下面
 5             int gap = stoi(version1) - stoi(version2);     //stoi()是库函数,将字符串转化为数字
 6             if (gap != 0) {
 7                 return gap > 0 ? 1 : -1;
 8             }
 9         }
10         return 0;
11     }
12
13     string nextSubstr(string str) {
14         for (int i = 0; i < str.size(); i++) {
15             if (str.at(i) == ‘.‘) {        //str.at(i) 等价于str[i]
16                 return str.substr(i + 1);    //substr()是字符函数,返回从i+1到字符串尾这段,即遇到‘.‘则
17             }
18         }
19         return "0";    //为什么返回的是字符串0????
20     }
21 };

  几个相关知识:

    1.str.at() : at是STL中的,str.at()等价于str[i],作用相同。

    2.substr() : substr(start, len)/获得字符串s中 从第‘start‘位开始的长度为‘len‘的字符串 //默认时的长度为从开始位置到尾

          substr(x)则是从x位开始到尾部

          http://blog.csdn.net/no_retreats/article/details/7853066

    3.stoi() : 将字符串转化为整型

  代码2的调试过程:不太明白是怎么处理的所以记录调试过程

 1 class MyClass {
 2 public:
 3     int compareVersion(string version1, string version2) {
 4         for (version1, version2; version1 != version2; version1 = nextSubstr(version1), version2 = nextSubstr(version2)) {
 5             cout << nextSubstr(version1) << endl;    //23   234
 6             cout << nextSubstr(version2) << endl;    //56  678
 7             int gap = stoi(version1) - stoi(version2);
 8             cout << stoi(version1) << endl;
 9             cout << stoi(version2) << endl;
10             if (gap != 0) {
11                 return gap > 0 ? 1 : -1;
12             }
13         }
14         return 0;
15     }
16
17     string nextSubstr(string str) {
18         for (size_t i = 0; i < str.size(); i++) {
19             if (str.at(i) == ‘.‘) {
20                 return str.substr(i + 1);
21                 //cout << i + 1 << endl;    //为什么在这里无法输出任何内容
22             }
23             //cout << i+1 << endl;
24         }
25         //cout << str.substr() << endl;
26         return "0";
27     }
28 };

  输入:1.23 3.45                输入:1.234  5.678       

          

  由此可以看出for循环  第一次循环‘.‘之前的内容,进行判断

            第二次循环‘.‘之后的内容。进行判断

  nextSubstr() 只对小数点之后的进行操作。

  疑问??为什么在第26行返回的是"0",而不是数字0,应为条用时是version 而version为string,所以返回值必须为string,但是改为0,运行没有出错。

  自己的测试代码:带有main函数,测试通过

  

时间: 2024-10-13 12:17:37

2016.5.21——Compare Version Numbers的相关文章

【leetcode】Compare Version Numbers

Compare Version Numbers Compare two version numbers version1 and version1.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 . 

No.165 Compare Version Numbers

No.165 Compare Version NumbersCompare 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

LeetCode:Compare Version Numbers - 比较版本号

1.题目名称 Compare Version Numbers(比较版本号) 2.题目地址 https://leetcode.com/problems/compare-version-numbers/ 3.题目内容 英文:Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. 中

【leetcode 字符串处理】Compare Version Numbers

[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume

leetCode 165. Compare Version Numbers 字符串

165. 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 t

leetcode_165题——Compare Version Numbers(string)

Compare Version Numbers Total Accepted: 19548 Total Submissions: 130867My Submissions Question Solution Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may

leetcode 152: Compare Version Numbers

Compare Version Numbers Total Accepted: 2468 Total Submissions: 17382 Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings a

165. Compare Version Numbers - LeetCode

Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: public int compareVersion(String version1, String version2) { String[] v1Arr = version1.split("\\."); String[] v2Arr = version2.split("\\.&qu

leetcode 165. 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 . charac