Leetcode 165. Compare Version Numbers

Source: Internet
Author: User

It seems a bit of a headache, but it's easier to figure out the rules:

1. The version number is different from the current number, then compare, return-1 or 1

2. The version number is the same as the current number, then the subsequent number is compared. such as 1.1:1 large, but 1.0 and 1 are the same; but 1.0.1 is bigger than 1.


It's not too long to write a good code: D.

Class Solution {Public:int compareversion (string version1, String version2) {while (Version1! = "" | | Version2 = "") {size_t dot1 = Version1.find ('. '), Dot2 = Version2.find ('. '); int level1 = string_to_int (version1.substr (0, DOT1)), int level2 = String_to_int (version2.substr (0, Dot2)); version1 = Dot1! = String::npos? VERSION1.SUBSTR (dot1 + 1): ""; version2 = dot2! = String::npos? VERSION2.SUBSTR (Dot2 + 1): "", if (level1! = Level2) {return level1<level2? -1:1;}} return 0;} private:inline int string_to_int (const string& str) {if (str = = "") {return 0;} int Ret;stringstream SS (str); SS >> ret;return ret;}};


Leetcode 165. Compare Version Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.