To the Top
File:  root - text - article - 2020 - 02 - compare-version-numbers.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 239 Views, 19373 Search Bots | 192 Words

Subscribe to Feed Burner | Browse | Archive
Hi, here's your problem today. This problem was recently asked by Amazon:

Version numbers are strings that are used to identify unique states of software products. A version number is in the format a.b.c.d. and so on where a, b, etc. are numeric strings separated by dots. These generally represent a hierarchy from major to minor changes. Given two version numbers version1 and version2, conclude which is the latest version number. Your code should do the following:
If version1 > version2 return 1.
If version1 < version2 return -1.
Otherwise return 0.

Note that the numeric strings such as a, b, c, d, etc. may have leading zeroes, and that the version strings do not start or end with dots. Unspecified level revision numbers default to 0.

Example:
Input:
version1 = "1.0.33"
version2 = "1.0.27"
Output: 1
#version1 > version2

Input:
version1 = "0.1"
version2 = "1.1"
Output: -1
#version1 < version2

Input:
version1 = "1.01"
version2 = "1.001"
Output: 0
#ignore leading zeroes, 01 and 001 represent the same number.

Input:
version1 = "1.0"
version2 = "1.0.0"
Output: 0
#version1 does not have a 3rd level revision number, which
defaults to "0"
Here's a starting point


class Solution:
def compareVersion(self, version1, version2):
# Fill this in.

version1 = "1.0.1"
version2 = "1"
print(Solution().compareVersion(version1, version2))
# 1
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 239 Views, 19373 Search Bots | 192 Words Subscribe to Feed Burner

Related Articles

  1. Absolute Path
  2. Kaprekar
  3. Sort a Partially Sorted List
  4. Daily Interview Problem: Spiral Traversal of Grid
  5. [Daily Problem] Add two numbers as a linked list
  6. Daily Interview Problem: Largest BST in a Binary Tree
  7. Daily Interview Puzzle: Ways to Traverse a Grid
  8. Daily Interview Problem: Room scheduling
  9. Daily Interview Problem: Jump to the End
  10. Maximum In A Stack

Comments (0)

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />