Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

Code shrimp is a sand carving and funny boy who likes listening to music, playing games and writing as well as most of his friends. The days are still very long, let’s refuel our efforts together 🌈


⭐ topic

Power button link

I give you two version numbers version1 and version2, please compare them. The version number consists of one or more revision numbers, and each revision number consists of one'. 'The connection. Each revision number is determined byA number of digitalComposed, may containLeading zeros. Each version number contains at least one character. Revision numbers are numbered from left to right, with subscripts 0, the leftmost revision number subscript 0, the next revision number subscript 1, and so on. For example,2.5.330.1Both are valid version numbers.

When comparing version numbers, compare their revision numbers from left to right. When comparing revision numbers, just compareIgnore any integer values that follow a leading zero. In other words, revision number1And revision number001 equal. If the version number does not specify a revision number at a subscript, the revision number is treated as 0. For example, version 1.0 is less than version 1.1 because they have the same revision number with subscript 0 and the revision number with subscript 1010 < 1

The return rule is as follows:

Return 1 if version1 > version2, -1 if version1 < version2, and 0 otherwise.

Example 1:

Input: version1 = “1.01”, version2 = “1.001” Output: 0 Explanation: Ignore leading zeros, “01” and “001” both represent the same integer “1”

Example 2:

Input: version1 = “1.0”, version2 = “1.0.0” Output: 0

Example 3:

Input: version1 = “0.1”, version2 = “1.1”

Output: 1

Explanation: the subscript 0 revision number in version1 is “0” and the subscript 0 revision number in version2 is “1”. 0 < 1, so version1 < version2

Example 4:

Input: version1 = “1.0.1”, version2 = “1”

Output: 1.

Example 5:

Input: version1 = “7.5.2.4”, version2 = “7.5.3”

Output: 1

Tip:

1 <= version1.length, Version2. Length <= 500 Version1 and version2 contain only numbers and ‘.’ Both version1 and version2 are valid version numbers version1 and version2 all revision numbers can be stored in 32 An integer in



🔥

It’s too cumbersome to manipulate strings, so let’s convert the string to a char array

So two strings will give you twocharAn array of

Then use the double pointer to search, encountered.Just stop. Of course, when we iterate over the pointer, we need to computeintValue, need to compare!

This method does not care about leading zeros, because if there are leading zeros, it will ignore them when calculating int values.



😊 Full code

class Solution {

    public int compareVersion(String version1, String version2) {
        // Convert to char array
        char[] tmp1 = version1.toCharArray();
        char[] tmp2 = version2.toCharArray();

        int len1 = tmp1.length,len2 = tmp2.length;

        / / double pointer
        int index1 = 0,index2 = 0;
        // Record the int value
        int sum1 = 0,sum2 = 0;
        while(index1 < len1 || index2 < len2) {
            // Set the value to 0 each time
            sum1 = 0;
            sum2 = 0;
            while(index1 < len1 && tmp1[index1] ! ='. ') {
                sum1 = sum1 * 10 + tmp1[index1] - '0';
                index1++;
            }
            while(index2 < len2 && tmp2[index2] ! ='. ') {
                sum2 = sum2 * 10 + tmp2[index2] - '0';
                index2++;
            }
            // If not
            if(sum1 ! = sum2) {return sum1 > sum2 ? 1 : -1;
            }
            Index1 ++, index2++, then skip. No.
            index1++;
            index2++;
        }
        return 0; }}Copy the code

The results



💖 finally

I am aCode pipi shrimp, a prawns lover who loves to share knowledge, will update useful blog posts in the future, looking forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == one key three even oh! Thank you for your support. See you next time

🔥原文 link: ✨


😉 offer yourself

Self-introduction, to everyone recommend their column 😁, welcome small partners to collect attention 😊

Force link algorithm problem solving area

The small white to learn Java

MybatisPlus column

App crawler Column

PC side crawler column

Big factory interview question column