LeetCode-Compare Version Numbers解题报告

原创 2015年07月06日 18:23:52

原题链接https://leetcode.com/problems/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

字符串处理而已 没什么难度


class Solution {
public:
    int compareVersion(string version1, string version2) {
		int p1 = 0, p2 = 0, i1, i2;
		while (p1 < version1.length() || p2 < version2.length())
		{
			i1 = sti(version1, p1);
			i2 = sti(version2, p2);
			if (i1 > i2)return 1;
			else if (i1 < i2)return -1;
			continue;
		}
		return 0;

	}
	int sti(string& s, int& pos)
	{
		int res = 0;
		while (pos < s.length() && s[pos] != '.')
		{
			res = res * 10 + (s[pos] - '0');
			pos++;
		}
		pos++;
		return res;
	}
};




相关文章推荐

【LeetCode】165. Compare Version Numbers 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51429823Subject 出处:https://leetcode.com/pro...

[leetcode] 315. Count of Smaller Numbers After Self 解题报告

题目链接: https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer ...

LeetCode 421 Maximum XOR of Two Numbers in an Array 解题报告

一道有意思的题目, 用到了异或运算的一个小特性. LeetCode 421 Maximum XOR of Two Numbers in an Array. Given a non-empty arra...

Leetcode 357. Count Numbers with Unique Digits 统计没有重复数字的数 解题报告

1 解题思想这道题其实是一个高中数学题(其实也是考研概率论的题目) 其做法也就是一个排列组合的关系,因为我们需要统计0~10^n里面不包含重复数字的所有数字,所以其实很简单: 对于位数为i的情况 ...
  • MebiuW
  • MebiuW
  • 2016年07月06日 20:01
  • 764

leetcode解题报告2. Add Two Numbers

leetcode解题报告2. Add Two Numbers题目地址 难度是medium题目描述用单链表来表示一个大整数,其中单链表的每个节点只有一个数0~9。给两个这样单链表表示的整数,求它们的和(...

[LeetCode] Add Two Numbers 解题报告

—— write for my baby, mua [题目] You are given two linked lists representing two non-negative numb...

【LeetCode】2. Add Two Numbers 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937Subject 出处:https://leetcode.com/pro...

[Leetcode] 201. Bitwise AND of Numbers Range 解题报告

题目: Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4. ...

LeetCode 2. Add Two Numbers 解题报告

题意: 有两个链表,它们表示逆序的两个非负数。例 (2 -> 4 -> 3)表示342,求两个数字的和,并用同样的方式逆序输出。如342+465 = 807,你需要把结果表达为(7 ->0 ->8)...

【leetcode 字符串处理】Compare Version Numbers

leetcode新题,Compare Version Numbers,字符串处理。题意:题意很清晰,就是比较“版本号”大小,给定的版本号version1和version2是字符串类型的,当versio...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode-Compare Version Numbers解题报告
举报原因:
原因补充:

(最多只允许输入30个字)