[Leetcode] Compare Version Numbers

Source: Internet
Author: User

My practice is to convert to vector re-processing, a variety of situations is relatively simple.

Class Solution {Public:int compareversion (string version1, String version2) {vector<int> V1 = convert (ve        Rsion1);        Vector<int> v2 = convert (Version2);        int size = min (V1.size (), v2.size ());            for (int i = 0; i < size; i++) {if (V1[i] < V2[i]) {return-1;            } else if (V1[i] > V2[i]) {return 1;        }} if (V1.size () > Size) return 1;        if (V2.size () > Size) return-1;    return 0;        } vector<int> Convert (String &version) {vector<int> result;        int len = Version.size ();        int num = 0; for (int i = 0; i < len; i++) {if (version[i] = = '. ')                {result.push_back (num);            num = 0;            } else {num = num * + (Version[i]-' 0 ');        }} result.push_back (num);    while (result.back () = = 0) {result.pop_back ();    } return result; }};

  

[Leetcode] 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.