Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 165 · Parsing / Conversion

Compare Version Numbers

LeetCode 165 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Parse revisions in lockstep; missing = 0.

ಮಧ್ಯಮTwo PointersString
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Compare Version Numbers (LeetCode 165) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Parsing / Conversion ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Compare revision by revision. Parse each revision as a number on the fly (so leading zeros vanish), treat missing revisions as 0, and return at the first difference.

Approach

  1. 1

    Pointers i and j into each version string.

  2. 2

    Read digits up to the next '.' into n1 and n2.

  3. 3

    Different → return 1 or −1; equal → skip the dots and continue.

  4. 4

    All equal → 0.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Two-pointer parsePythonO(m+n) time · O(1) space
class Solution:
    def compareVersion(self, version1, version2):
        i = 0
        j = 0
        v1len = len(version1)
        v2len = len(version2)
        while i < v1len or j < v2len:
            n1 = 0
            n2 = 0
            while i < v1len and version1[i] != ".":
                n1 = n1 * 10 + int(version1[i])
                i += 1
            while j < v2len and version2[j] != ".":
                n2 = n2 * 10 + int(version2[j])
                j += 1
            if n1 > n2:
                return 1
            elif n1 < n2:
                return -1
            i += 1
            j += 1
        return 0
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Compare Version Numbers — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Compare Version Numbers ಹಿಂದಿನ intuition ಏನು?

Compare revision by revision. Parse each revision as a number on the fly (so leading zeros vanish), treat missing revisions as 0, and return at the first difference.

Compare Version Numbers (LeetCode 165) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Pointers i and j into each version string. 2. Read digits up to the next '.' into n1 and n2. 3. Different → return 1 or −1; equal → skip the dots and continue. 4. All equal → 0.

Compare Version Numbers ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(m + n). Space: O(1). (Accepted approach: Two-pointer parse.)

Compare Version Numbers ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

'1.0' and '1.0.0.0' are equal.